roxify 1.2.7 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/pack.js CHANGED
@@ -1,188 +1,188 @@
1
- import { readFileSync, readdirSync, statSync } from 'fs';
2
- import { extname, join, relative, resolve, sep } from 'path';
3
- function* collectFilesGenerator(paths) {
4
- for (const p of paths) {
5
- const abs = resolve(p);
6
- const st = statSync(abs);
7
- if (st.isFile()) {
8
- yield abs;
9
- }
10
- else if (st.isDirectory()) {
11
- const names = readdirSync(abs);
12
- const childPaths = names.map((n) => join(abs, n));
13
- yield* collectFilesGenerator(childPaths);
14
- }
15
- }
16
- }
17
- export function packPathsToParts(paths, baseDir, onProgress) {
18
- const files = [];
19
- for (const f of collectFilesGenerator(paths)) {
20
- files.push(f);
21
- }
22
- const base = baseDir ? resolve(baseDir) : process.cwd();
23
- const parts = [];
24
- const list = [];
25
- let total = 0;
26
- const sizes = files.map((f) => {
27
- const st = statSync(f);
28
- total += st.size;
29
- return st.size;
30
- });
31
- let readSoFar = 0;
32
- for (let idx = 0; idx < files.length; idx++) {
33
- const f = files[idx];
34
- const rel = relative(base, f).split(sep).join('/');
35
- const content = readFileSync(f);
36
- const nameBuf = Buffer.from(rel, 'utf8');
37
- const nameLen = Buffer.alloc(2);
38
- nameLen.writeUInt16BE(nameBuf.length, 0);
39
- const sizeBuf = Buffer.alloc(8);
40
- sizeBuf.writeBigUInt64BE(BigInt(content.length), 0);
41
- parts.push(nameLen, nameBuf, sizeBuf, content);
42
- list.push(rel);
43
- readSoFar += sizes[idx];
44
- if (onProgress)
45
- onProgress(readSoFar, total, rel);
46
- }
47
- const header = Buffer.alloc(8);
48
- header.writeUInt32BE(0x524f5850, 0);
49
- header.writeUInt32BE(files.length, 4);
50
- parts.unshift(header);
51
- return { parts, list };
52
- }
53
- export function packPaths(paths, baseDir, onProgress) {
54
- const { parts, list } = packPathsToParts(paths, baseDir, onProgress);
55
- return { buf: Buffer.concat(parts), list };
56
- }
57
- export function unpackBuffer(buf, fileList) {
58
- if (buf.length < 8)
59
- return null;
60
- const magic = buf.readUInt32BE(0);
61
- if (magic === 0x524f5849) {
62
- const indexLen = buf.readUInt32BE(4);
63
- const indexBuf = buf.slice(8, 8 + indexLen);
64
- const index = JSON.parse(indexBuf.toString('utf8'));
65
- const dataStart = 8 + indexLen;
66
- const files = [];
67
- const entriesToProcess = fileList
68
- ? index.filter((e) => fileList.includes(e.path))
69
- : index;
70
- for (const entry of entriesToProcess) {
71
- const entryStart = dataStart + entry.offset;
72
- let ptr = entryStart;
73
- if (ptr + 2 > buf.length)
74
- continue;
75
- const nameLen = buf.readUInt16BE(ptr);
76
- ptr += 2;
77
- ptr += nameLen;
78
- ptr += 8;
79
- if (ptr + entry.size > buf.length)
80
- continue;
81
- const content = buf.slice(ptr, ptr + entry.size);
82
- files.push({ path: entry.path, buf: content });
83
- }
84
- return { files };
85
- }
86
- if (magic !== 0x524f5850)
87
- return null;
88
- const header = buf.slice(0, 8);
89
- const fileCount = header.readUInt32BE(4);
90
- let offset = 8;
91
- const files = [];
92
- for (let i = 0; i < fileCount; i++) {
93
- if (offset + 2 > buf.length)
94
- return null;
95
- const nameLen = buf.readUInt16BE(offset);
96
- offset += 2;
97
- if (offset + nameLen > buf.length)
98
- return null;
99
- const name = buf.slice(offset, offset + nameLen).toString('utf8');
100
- offset += nameLen;
101
- if (offset + 8 > buf.length)
102
- return null;
103
- const size = buf.readBigUInt64BE(offset);
104
- offset += 8;
105
- if (offset + Number(size) > buf.length)
106
- return null;
107
- const content = buf.slice(offset, offset + Number(size));
108
- offset += Number(size);
109
- files.push({ path: name, buf: content });
110
- }
111
- if (fileList) {
112
- const filtered = files.filter((f) => fileList.includes(f.path));
113
- return { files: filtered };
114
- }
115
- return { files };
116
- }
117
- export async function packPathsGenerator(paths, baseDir, onProgress) {
118
- const files = [];
119
- for (const f of collectFilesGenerator(paths)) {
120
- files.push(f);
121
- }
122
- files.sort((a, b) => {
123
- const extA = extname(a);
124
- const extB = extname(b);
125
- if (extA !== extB)
126
- return extA.localeCompare(extB);
127
- return a.localeCompare(b);
128
- });
129
- const base = baseDir ? resolve(baseDir) : process.cwd();
130
- const BLOCK_SIZE = 8 * 1024 * 1024;
131
- const index = [];
132
- let currentBlockId = 0;
133
- let currentBlockSize = 0;
134
- let globalDataOffset = 0;
135
- let totalSize = 0;
136
- for (const f of files) {
137
- const st = statSync(f);
138
- const rel = relative(base, f).split(sep).join('/');
139
- const nameBuf = Buffer.from(rel, 'utf8');
140
- const entrySize = 2 + nameBuf.length + 8 + st.size;
141
- if (currentBlockSize + entrySize > BLOCK_SIZE && currentBlockSize > 0) {
142
- currentBlockId++;
143
- currentBlockSize = 0;
144
- }
145
- index.push({
146
- path: rel,
147
- blockId: currentBlockId,
148
- offset: globalDataOffset,
149
- size: st.size,
150
- });
151
- currentBlockSize += entrySize;
152
- globalDataOffset += entrySize;
153
- totalSize += st.size;
154
- }
155
- async function* streamGenerator() {
156
- const indexBuf = Buffer.from(JSON.stringify(index), 'utf8');
157
- const indexHeader = Buffer.alloc(8);
158
- indexHeader.writeUInt32BE(0x524f5849, 0);
159
- indexHeader.writeUInt32BE(indexBuf.length, 4);
160
- yield Buffer.concat([indexHeader, indexBuf]);
161
- let currentBuffer = Buffer.alloc(0);
162
- let readSoFar = 0;
163
- for (let i = 0; i < files.length; i++) {
164
- const f = files[i];
165
- const rel = relative(base, f).split(sep).join('/');
166
- const content = readFileSync(f);
167
- const nameBuf = Buffer.from(rel, 'utf8');
168
- const nameLen = Buffer.alloc(2);
169
- nameLen.writeUInt16BE(nameBuf.length, 0);
170
- const sizeBuf = Buffer.alloc(8);
171
- sizeBuf.writeBigUInt64BE(BigInt(content.length), 0);
172
- const entry = Buffer.concat([nameLen, nameBuf, sizeBuf, content]);
173
- if (currentBuffer.length + entry.length > BLOCK_SIZE &&
174
- currentBuffer.length > 0) {
175
- yield currentBuffer;
176
- currentBuffer = Buffer.alloc(0);
177
- }
178
- currentBuffer = Buffer.concat([currentBuffer, entry]);
179
- readSoFar += content.length;
180
- if (onProgress)
181
- onProgress(readSoFar, totalSize, rel);
182
- }
183
- if (currentBuffer.length > 0) {
184
- yield currentBuffer;
185
- }
186
- }
187
- return { index, stream: streamGenerator(), totalSize };
188
- }
1
+ import { readFileSync, readdirSync, statSync } from 'fs';
2
+ import { extname, join, relative, resolve, sep } from 'path';
3
+ function* collectFilesGenerator(paths) {
4
+ for (const p of paths) {
5
+ const abs = resolve(p);
6
+ const st = statSync(abs);
7
+ if (st.isFile()) {
8
+ yield abs;
9
+ }
10
+ else if (st.isDirectory()) {
11
+ const names = readdirSync(abs);
12
+ const childPaths = names.map((n) => join(abs, n));
13
+ yield* collectFilesGenerator(childPaths);
14
+ }
15
+ }
16
+ }
17
+ export function packPathsToParts(paths, baseDir, onProgress) {
18
+ const files = [];
19
+ for (const f of collectFilesGenerator(paths)) {
20
+ files.push(f);
21
+ }
22
+ const base = baseDir ? resolve(baseDir) : process.cwd();
23
+ const parts = [];
24
+ const list = [];
25
+ let total = 0;
26
+ const sizes = files.map((f) => {
27
+ const st = statSync(f);
28
+ total += st.size;
29
+ return st.size;
30
+ });
31
+ let readSoFar = 0;
32
+ for (let idx = 0; idx < files.length; idx++) {
33
+ const f = files[idx];
34
+ const rel = relative(base, f).split(sep).join('/');
35
+ const content = readFileSync(f);
36
+ const nameBuf = Buffer.from(rel, 'utf8');
37
+ const nameLen = Buffer.alloc(2);
38
+ nameLen.writeUInt16BE(nameBuf.length, 0);
39
+ const sizeBuf = Buffer.alloc(8);
40
+ sizeBuf.writeBigUInt64BE(BigInt(content.length), 0);
41
+ parts.push(nameLen, nameBuf, sizeBuf, content);
42
+ list.push(rel);
43
+ readSoFar += sizes[idx];
44
+ if (onProgress)
45
+ onProgress(readSoFar, total, rel);
46
+ }
47
+ const header = Buffer.alloc(8);
48
+ header.writeUInt32BE(0x524f5850, 0);
49
+ header.writeUInt32BE(files.length, 4);
50
+ parts.unshift(header);
51
+ return { parts, list };
52
+ }
53
+ export function packPaths(paths, baseDir, onProgress) {
54
+ const { parts, list } = packPathsToParts(paths, baseDir, onProgress);
55
+ return { buf: Buffer.concat(parts), list };
56
+ }
57
+ export function unpackBuffer(buf, fileList) {
58
+ if (buf.length < 8)
59
+ return null;
60
+ const magic = buf.readUInt32BE(0);
61
+ if (magic === 0x524f5849) {
62
+ const indexLen = buf.readUInt32BE(4);
63
+ const indexBuf = buf.slice(8, 8 + indexLen);
64
+ const index = JSON.parse(indexBuf.toString('utf8'));
65
+ const dataStart = 8 + indexLen;
66
+ const files = [];
67
+ const entriesToProcess = fileList
68
+ ? index.filter((e) => fileList.includes(e.path))
69
+ : index;
70
+ for (const entry of entriesToProcess) {
71
+ const entryStart = dataStart + entry.offset;
72
+ let ptr = entryStart;
73
+ if (ptr + 2 > buf.length)
74
+ continue;
75
+ const nameLen = buf.readUInt16BE(ptr);
76
+ ptr += 2;
77
+ ptr += nameLen;
78
+ ptr += 8;
79
+ if (ptr + entry.size > buf.length)
80
+ continue;
81
+ const content = buf.slice(ptr, ptr + entry.size);
82
+ files.push({ path: entry.path, buf: content });
83
+ }
84
+ return { files };
85
+ }
86
+ if (magic !== 0x524f5850)
87
+ return null;
88
+ const header = buf.slice(0, 8);
89
+ const fileCount = header.readUInt32BE(4);
90
+ let offset = 8;
91
+ const files = [];
92
+ for (let i = 0; i < fileCount; i++) {
93
+ if (offset + 2 > buf.length)
94
+ return null;
95
+ const nameLen = buf.readUInt16BE(offset);
96
+ offset += 2;
97
+ if (offset + nameLen > buf.length)
98
+ return null;
99
+ const name = buf.slice(offset, offset + nameLen).toString('utf8');
100
+ offset += nameLen;
101
+ if (offset + 8 > buf.length)
102
+ return null;
103
+ const size = buf.readBigUInt64BE(offset);
104
+ offset += 8;
105
+ if (offset + Number(size) > buf.length)
106
+ return null;
107
+ const content = buf.slice(offset, offset + Number(size));
108
+ offset += Number(size);
109
+ files.push({ path: name, buf: content });
110
+ }
111
+ if (fileList) {
112
+ const filtered = files.filter((f) => fileList.includes(f.path));
113
+ return { files: filtered };
114
+ }
115
+ return { files };
116
+ }
117
+ export async function packPathsGenerator(paths, baseDir, onProgress) {
118
+ const files = [];
119
+ for (const f of collectFilesGenerator(paths)) {
120
+ files.push(f);
121
+ }
122
+ files.sort((a, b) => {
123
+ const extA = extname(a);
124
+ const extB = extname(b);
125
+ if (extA !== extB)
126
+ return extA.localeCompare(extB);
127
+ return a.localeCompare(b);
128
+ });
129
+ const base = baseDir ? resolve(baseDir) : process.cwd();
130
+ const BLOCK_SIZE = 8 * 1024 * 1024;
131
+ const index = [];
132
+ let currentBlockId = 0;
133
+ let currentBlockSize = 0;
134
+ let globalDataOffset = 0;
135
+ let totalSize = 0;
136
+ for (const f of files) {
137
+ const st = statSync(f);
138
+ const rel = relative(base, f).split(sep).join('/');
139
+ const nameBuf = Buffer.from(rel, 'utf8');
140
+ const entrySize = 2 + nameBuf.length + 8 + st.size;
141
+ if (currentBlockSize + entrySize > BLOCK_SIZE && currentBlockSize > 0) {
142
+ currentBlockId++;
143
+ currentBlockSize = 0;
144
+ }
145
+ index.push({
146
+ path: rel,
147
+ blockId: currentBlockId,
148
+ offset: globalDataOffset,
149
+ size: st.size,
150
+ });
151
+ currentBlockSize += entrySize;
152
+ globalDataOffset += entrySize;
153
+ totalSize += st.size;
154
+ }
155
+ async function* streamGenerator() {
156
+ const indexBuf = Buffer.from(JSON.stringify(index), 'utf8');
157
+ const indexHeader = Buffer.alloc(8);
158
+ indexHeader.writeUInt32BE(0x524f5849, 0);
159
+ indexHeader.writeUInt32BE(indexBuf.length, 4);
160
+ yield Buffer.concat([indexHeader, indexBuf]);
161
+ let currentBuffer = Buffer.alloc(0);
162
+ let readSoFar = 0;
163
+ for (let i = 0; i < files.length; i++) {
164
+ const f = files[i];
165
+ const rel = relative(base, f).split(sep).join('/');
166
+ const content = readFileSync(f);
167
+ const nameBuf = Buffer.from(rel, 'utf8');
168
+ const nameLen = Buffer.alloc(2);
169
+ nameLen.writeUInt16BE(nameBuf.length, 0);
170
+ const sizeBuf = Buffer.alloc(8);
171
+ sizeBuf.writeBigUInt64BE(BigInt(content.length), 0);
172
+ const entry = Buffer.concat([nameLen, nameBuf, sizeBuf, content]);
173
+ if (currentBuffer.length + entry.length > BLOCK_SIZE &&
174
+ currentBuffer.length > 0) {
175
+ yield currentBuffer;
176
+ currentBuffer = Buffer.alloc(0);
177
+ }
178
+ currentBuffer = Buffer.concat([currentBuffer, entry]);
179
+ readSoFar += content.length;
180
+ if (onProgress)
181
+ onProgress(readSoFar, totalSize, rel);
182
+ }
183
+ if (currentBuffer.length > 0) {
184
+ yield currentBuffer;
185
+ }
186
+ }
187
+ return { index, stream: streamGenerator(), totalSize };
188
+ }
@@ -1,38 +1,38 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- export declare const CHUNK_TYPE = "rXDT";
4
- export declare const MAGIC: Buffer;
5
- export declare const PIXEL_MAGIC: Buffer;
6
- export declare const ENC_NONE = 0;
7
- export declare const ENC_AES = 1;
8
- export declare const ENC_XOR = 2;
9
- export declare const FILTER_ZERO: Buffer;
10
- export declare const PNG_HEADER: Buffer;
11
- export declare const PNG_HEADER_HEX: string;
12
- export declare const MARKER_COLORS: {
13
- r: number;
14
- g: number;
15
- b: number;
16
- }[];
17
- export declare const MARKER_START: {
18
- r: number;
19
- g: number;
20
- b: number;
21
- }[];
22
- export declare const MARKER_END: {
23
- r: number;
24
- g: number;
25
- b: number;
26
- }[];
27
- export declare const COMPRESSION_MARKERS: {
28
- zstd: {
29
- r: number;
30
- g: number;
31
- b: number;
32
- }[];
33
- lzma: {
34
- r: number;
35
- g: number;
36
- b: number;
37
- }[];
38
- };
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ export declare const CHUNK_TYPE = "rXDT";
4
+ export declare const MAGIC: Buffer;
5
+ export declare const PIXEL_MAGIC: Buffer;
6
+ export declare const ENC_NONE = 0;
7
+ export declare const ENC_AES = 1;
8
+ export declare const ENC_XOR = 2;
9
+ export declare const FILTER_ZERO: Buffer;
10
+ export declare const PNG_HEADER: Buffer;
11
+ export declare const PNG_HEADER_HEX: string;
12
+ export declare const MARKER_COLORS: {
13
+ r: number;
14
+ g: number;
15
+ b: number;
16
+ }[];
17
+ export declare const MARKER_START: {
18
+ r: number;
19
+ g: number;
20
+ b: number;
21
+ }[];
22
+ export declare const MARKER_END: {
23
+ r: number;
24
+ g: number;
25
+ b: number;
26
+ }[];
27
+ export declare const COMPRESSION_MARKERS: {
28
+ zstd: {
29
+ r: number;
30
+ g: number;
31
+ b: number;
32
+ }[];
33
+ lzma: {
34
+ r: number;
35
+ g: number;
36
+ b: number;
37
+ }[];
38
+ };
@@ -1,22 +1,22 @@
1
- export const CHUNK_TYPE = 'rXDT';
2
- export const MAGIC = Buffer.from('ROX1');
3
- export const PIXEL_MAGIC = Buffer.from('PXL1');
4
- export const ENC_NONE = 0;
5
- export const ENC_AES = 1;
6
- export const ENC_XOR = 2;
7
- export const FILTER_ZERO = Buffer.from([0]);
8
- export const PNG_HEADER = Buffer.from([
9
- 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
10
- ]);
11
- export const PNG_HEADER_HEX = PNG_HEADER.toString('hex');
12
- export const MARKER_COLORS = [
13
- { r: 255, g: 0, b: 0 },
14
- { r: 0, g: 255, b: 0 },
15
- { r: 0, g: 0, b: 255 },
16
- ];
17
- export const MARKER_START = MARKER_COLORS;
18
- export const MARKER_END = [...MARKER_COLORS].reverse();
19
- export const COMPRESSION_MARKERS = {
20
- zstd: [{ r: 0, g: 255, b: 0 }],
21
- lzma: [{ r: 255, g: 255, b: 0 }],
22
- };
1
+ export const CHUNK_TYPE = 'rXDT';
2
+ export const MAGIC = Buffer.from('ROX1');
3
+ export const PIXEL_MAGIC = Buffer.from('PXL1');
4
+ export const ENC_NONE = 0;
5
+ export const ENC_AES = 1;
6
+ export const ENC_XOR = 2;
7
+ export const FILTER_ZERO = Buffer.from([0]);
8
+ export const PNG_HEADER = Buffer.from([
9
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
10
+ ]);
11
+ export const PNG_HEADER_HEX = PNG_HEADER.toString('hex');
12
+ export const MARKER_COLORS = [
13
+ { r: 255, g: 0, b: 0 },
14
+ { r: 0, g: 255, b: 0 },
15
+ { r: 0, g: 0, b: 255 },
16
+ ];
17
+ export const MARKER_START = MARKER_COLORS;
18
+ export const MARKER_END = [...MARKER_COLORS].reverse();
19
+ export const COMPRESSION_MARKERS = {
20
+ zstd: [{ r: 0, g: 255, b: 0 }],
21
+ lzma: [{ r: 255, g: 255, b: 0 }],
22
+ };
@@ -1,4 +1,4 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- export declare function crc32(buf: Buffer, previous?: number): number;
4
- export declare function adler32(buf: Buffer, prev?: number): number;
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ export declare function crc32(buf: Buffer, previous?: number): number;
4
+ export declare function adler32(buf: Buffer, prev?: number): number;
package/dist/utils/crc.js CHANGED
@@ -1,29 +1,29 @@
1
- const CRC_TABLE = [];
2
- for (let n = 0; n < 256; n++) {
3
- let c = n;
4
- for (let k = 0; k < 8; k++) {
5
- if (c & 1) {
6
- c = 0xedb88320 ^ (c >>> 1);
7
- }
8
- else {
9
- c = c >>> 1;
10
- }
11
- }
12
- CRC_TABLE[n] = c;
13
- }
14
- export function crc32(buf, previous = 0) {
15
- let crc = previous ^ 0xffffffff;
16
- for (let i = 0; i < buf.length; i++) {
17
- crc = CRC_TABLE[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
18
- }
19
- return (crc ^ 0xffffffff) >>> 0;
20
- }
21
- export function adler32(buf, prev = 1) {
22
- let s1 = prev & 0xffff;
23
- let s2 = (prev >>> 16) & 0xffff;
24
- for (let i = 0; i < buf.length; i++) {
25
- s1 = (s1 + buf[i]) % 65521;
26
- s2 = (s2 + s1) % 65521;
27
- }
28
- return ((s2 << 16) | s1) >>> 0;
29
- }
1
+ const CRC_TABLE = [];
2
+ for (let n = 0; n < 256; n++) {
3
+ let c = n;
4
+ for (let k = 0; k < 8; k++) {
5
+ if (c & 1) {
6
+ c = 0xedb88320 ^ (c >>> 1);
7
+ }
8
+ else {
9
+ c = c >>> 1;
10
+ }
11
+ }
12
+ CRC_TABLE[n] = c;
13
+ }
14
+ export function crc32(buf, previous = 0) {
15
+ let crc = previous ^ 0xffffffff;
16
+ for (let i = 0; i < buf.length; i++) {
17
+ crc = CRC_TABLE[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
18
+ }
19
+ return (crc ^ 0xffffffff) >>> 0;
20
+ }
21
+ export function adler32(buf, prev = 1) {
22
+ let s1 = prev & 0xffff;
23
+ let s2 = (prev >>> 16) & 0xffff;
24
+ for (let i = 0; i < buf.length; i++) {
25
+ s1 = (s1 + buf[i]) % 65521;
26
+ s2 = (s2 + s1) % 65521;
27
+ }
28
+ return ((s2 << 16) | s1) >>> 0;
29
+ }
@@ -1,4 +1,4 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { DecodeOptions, DecodeResult } from './types.js';
4
- export declare function decodePngToBinary(input: Buffer | string, opts?: DecodeOptions): Promise<DecodeResult>;
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { DecodeOptions, DecodeResult } from './types.js';
4
+ export declare function decodePngToBinary(input: Buffer | string, opts?: DecodeOptions): Promise<DecodeResult>;