@openzim/libzim 2.4.4 → 3.1.0

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/.env CHANGED
@@ -1 +1 @@
1
- LIBZIM_VERSION=6.3.2
1
+ LIBZIM_VERSION=8.2.1
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ .eslintrc.js
2
+ node_modules
3
+ dist/*
package/.eslintrc.js ADDED
@@ -0,0 +1,39 @@
1
+ module.exports = {
2
+ env: {
3
+ es6: true,
4
+ node: true,
5
+ },
6
+ rules: {
7
+ "prettier/prettier": "error",
8
+ eqeqeq: "error",
9
+ },
10
+ extends: [
11
+ "eslint:recommended",
12
+ "plugin:@typescript-eslint/eslint-recommended",
13
+ "plugin:@typescript-eslint/recommended",
14
+ "prettier",
15
+ ],
16
+ parser: "@typescript-eslint/parser",
17
+ parserOptions: {
18
+ project: "tsconfig.json",
19
+ sourceType: "module",
20
+ },
21
+ plugins: ["prettier"],
22
+ root: true,
23
+ overrides: [
24
+ {
25
+ files: ["*.ts", "*.js"],
26
+ rules: {
27
+ "@typescript-eslint/no-var-requires": "off",
28
+ "no-import-assign": "off",
29
+ "no-useless-escape": "off",
30
+ },
31
+ parserOptions: {
32
+ sourceType: "module",
33
+ },
34
+ extends: ["plugin:@typescript-eslint/recommended"],
35
+ parser: "@typescript-eslint/parser",
36
+ plugins: ["@typescript-eslint"],
37
+ },
38
+ ],
39
+ };
package/Changelog CHANGED
@@ -1,3 +1,16 @@
1
+ 3.1.0:
2
+ * NEW: Use libzim v8.2.1
3
+ * FIX: Handling of binary data in the creator
4
+ * FIX: Use ESLint instead of TSLint
5
+ * REMOVE: Node.js 16 from the CI
6
+
7
+ 3.0.0:
8
+ * NEW: Use libzim v8.2.0
9
+ * NEW: Calling API (to follow libzim changes)
10
+ * NEW: Add support of ARM(64) for both Linux and macOS
11
+ * UPDATE: Most of the dependencies
12
+ * UPDATE: Many improvements around the CI
13
+
1
14
  2.4.4:
2
15
  * NEW: Use libzim v6.3.2
3
16
 
package/README.md CHANGED
@@ -6,8 +6,8 @@ This is the Node.js binding to the
6
6
  [ZIM](https://openzim.org) files easily in Javascript.
7
7
 
8
8
  [![npm](https://img.shields.io/npm/v/@openzim/libzim.svg)](https://www.npmjs.com/package/@openzim/libzim)
9
- [![Build Status](https://github.com/openzim/node-libzim/workflows/CI/badge.svg?branch=master)](https://github.com/openzim/node-libzim/actions?query=branch%3Amaster)
10
- [![codecov](https://codecov.io/gh/openzim/node-libzim/branch/master/graph/badge.svg)](https://codecov.io/gh/openzim/node-libzim)
9
+ [![Build Status](https://github.com/openzim/node-libzim/workflows/CI/badge.svg?branch=main)](https://github.com/openzim/node-libzim/actions?query=branch%3Amain)
10
+ [![codecov](https://codecov.io/gh/openzim/node-libzim/branch/main/graph/badge.svg)](https://codecov.io/gh/openzim/node-libzim)
11
11
  [![CodeFactor](https://www.codefactor.io/repository/github/openzim/node-libzim/badge)](https://www.codefactor.io/repository/github/openzim/node-libzim)
12
12
  [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
13
13
 
@@ -22,55 +22,77 @@ here](https://github.com/openzim/libzim/)).
22
22
  ## Usage
23
23
 
24
24
  ```bash
25
- npm i openzim/libzim
25
+ npm i @openzim/libzim
26
26
  ```
27
27
 
28
- ### Writing a Zim file
28
+ ### Writing a ZIM file
29
29
  ```javascript
30
30
  // write.js
31
- const { ZimArticle, ZimCreator } = require("@openzim/libzim");
31
+ import { Creator, StringItem } from "@openzim/libzim";
32
32
 
33
33
  (async () => {
34
-
35
34
  console.info('Starting');
36
- const creator = new ZimCreator({ fileName: 'test.zim' }, { welcome: 'index.html' });
37
-
38
- for (let i = 100; i > 0; i--) {
39
- const a = new ZimArticle({ url: `file${i}`, data: `Content ${i}` });
40
- await creator.addArticle(a);
35
+ const outFile = "./test.zim";
36
+ const creator = new Creator()
37
+ .configNbWorkers(1)
38
+ .configIndexing(true, "en")
39
+ .configClusterSize(2048)
40
+ .startZimCreation(outFile);
41
+
42
+ for (let i = 0; i < 100; i++) {
43
+ const item = new StringItem(
44
+ `file${i}`, // path url
45
+ "text/plain", // content-type
46
+ `Title ${i}`, // title
47
+ {FRONT_ARTICLE: 1, COMPRESS: 1}, // hint option flags
48
+ `<h1>Content / Data ${i}</h1>` // content
49
+ );
50
+ await creator.addItem(item);
41
51
  }
42
52
 
43
- const welcome = new ZimArticle({ url: `index.html`, data: `<h1>Welcome!</h1>` });
44
- await creator.addArticle(welcome);
45
-
46
- await creator.finalise();
53
+ creator.setMainPath("file0");
54
+ await creator.finishZimCreation();
47
55
 
48
56
  console.log('Done Writing');
49
-
50
57
  })();
51
58
  ```
52
59
 
53
- ### Reading a Zim file
60
+ ### Reading a ZIM file
54
61
  ```javascript
55
62
  // read.js
56
-
57
- const { ZimArticle, ZimReader } = require("@openzim/libzim");
63
+ import { Archive, SuggestionSearcher, Searcher } from "@openzim/libzim";
58
64
 
59
65
  (async () => {
66
+ const outFile = "./test.zim";
67
+ const archive = new Archive(outFile);
68
+ console.log(`Archive opened: main entry path - ${archive.mainEntry.path}`);
60
69
 
61
- const zimFile = new ZimReader(path.join(__dirname, '../test.zim'));
62
-
63
- const suggestResults = await zimFile.suggest('laborum');
64
- console.info(`Suggest Results:`, suggestResults);
70
+ for (const entry of archive.iterByPath()) {
71
+ console.log(`entry: ${entry.path} - ${entry.title}`);
72
+ }
65
73
 
66
- const searchResults = await zimFile.search('rem');
67
- console.info(`Search Results:`, searchResults);
74
+ const suggestionSearcher = new SuggestionSearcher(archive);
75
+ const suggestion = suggestionSearcher.suggest('laborum');
76
+ let results = suggestion.getResults(0, 10);
77
+ console.log("Suggestion results:");
78
+ for(const entry of results) {
79
+ console.log(`\t- ${entry.path} - ${entry.title}`);
80
+ }
68
81
 
69
- const readArticleContent = await zimFile.getArticleByUrl('A/laborum');
70
- console.info(`Article by url (laborum):`, readArticleContent);
82
+ const searcher = new Searcher(archive);
83
+ const search = searcher.search(new Query('rem'));
84
+ results = search.getResults(0, 10);
85
+ console.log("Search results:");
86
+ for(const entry of results) {
87
+ console.log(`\t- ${entry.path} - ${entry.title}`);
88
+ }
71
89
 
72
- await zimFile.destroy();
73
90
 
91
+ const entry = await archive.getEntryByPath("A/laborum");
92
+ const item = entry.item;
93
+ const blob = item.data;
94
+ console.info(`Entry by url (laborum):`, blob.data);
95
+ delete archive;
74
96
  })();
75
97
 
76
98
  ```
package/binding.gyp CHANGED
@@ -24,7 +24,7 @@
24
24
  "libraries": [
25
25
  "-Wl,-rpath,'$$ORIGIN'",
26
26
  "-L<(libzim_dir)/lib/x86_64-linux-gnu",
27
- "<(libzim_dir)/lib/x86_64-linux-gnu/libzim.so.6",
27
+ "<(libzim_dir)/lib/x86_64-linux-gnu/libzim.so.8",
28
28
  ],
29
29
  }],
30
30
  ["libzim_local!='true' and OS=='mac'", {
@@ -33,7 +33,8 @@
33
33
  "xcode_settings": {
34
34
  "GCC_SYMBOLS_PRIVATE_EXTERN": "YES", # -fvisibility=hidden
35
35
  "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
36
- "LD_RUNPATH_SEARCH_PATHS": "@loader_path/"
36
+ "LD_RUNPATH_SEARCH_PATHS": "@loader_path/",
37
+ "OTHER_CFLAGS": [ "-std=c++17", "-fexceptions" ],
37
38
  },
38
39
  "libraries": ["-Wl,-rpath,@loader_path/", "-L<(libzim_dir)/lib", "-lzim"],
39
40
  }],
@@ -45,9 +46,6 @@
45
46
  "cflags_cc": [ "-std=c++17", "-fexceptions" ],
46
47
  "sources": [
47
48
  "src/module.cc",
48
- "src/article.cc",
49
- "src/reader.cc",
50
- "src/writer.cc",
51
49
  ],
52
50
  "include_dirs": [
53
51
  "<!@(node -p \"require('node-addon-api').include\")",
package/bundle-libzim.js CHANGED
@@ -1,26 +1,32 @@
1
- require('dotenv').config();
2
- const mkdirp = require('mkdirp');
3
- const exec = require('exec-then');
4
- const os = require('os');
1
+ require("dotenv").config();
2
+ const mkdirp = require("mkdirp");
3
+ const exec = require("exec-then");
4
+ const os = require("os");
5
5
 
6
- mkdirp.sync('./build/Release');
6
+ mkdirp.sync("./build/Release");
7
7
 
8
- const isMacOS = os.type() === 'Darwin'
9
- const isLinux = os.type() === 'Linux'
8
+ const isMacOS = os.type() === "Darwin";
9
+ const isLinux = os.type() === "Linux";
10
10
 
11
11
  if (!isMacOS && !isLinux) {
12
- console.warn(`\x1b[41m\n================================ README \n\nlibzim bundle with prebuilt binaries only available for macOS and Linux:\n\n\thttps://github.com/openzim/libzim/\n\n================================\x1b[0m\n`);
12
+ console.warn(
13
+ "\x1b[41m\n================================ README \n\nlibzim bundle with prebuilt binaries only available for macOS and Linux:\n\n\thttps://github.com/openzim/libzim/\n\n================================\x1b[0m\n"
14
+ );
13
15
  }
14
16
 
15
17
  if (isLinux) {
16
- console.info(`Copying libzim.so.6 to build folder`)
17
- exec(`cp download/lib/x86_64-linux-gnu/libzim.so.6 build/Release/libzim.so.6`)
18
- exec(`ln -sf build/Release/libzim.so.6 build/Release/libzim.so`) // convienience only, not required
18
+ console.info("Copying libzim.so.8 to build folder");
19
+ exec(
20
+ "cp download/lib/x86_64-linux-gnu/libzim.so.8 build/Release/libzim.so.8"
21
+ );
22
+ exec("ln -sf build/Release/libzim.so.8 build/Release/libzim.so"); // convienience only, not required
19
23
  }
20
24
  if (isMacOS) {
21
- console.info(`Copying libzim.6.dylib to build folder`);
22
- exec(`cp download/lib/libzim.6.dylib build/Release/libzim.6.dylib`)
23
- exec(`ln -sf build/Release/libzim.6.dylib build/Release/libzim.dylib`) // convienience only, not required
24
- console.info(`Fixing rpath`)
25
- exec(`install_name_tool -change libzim.6.dylib @loader_path/libzim.6.dylib build/Release/zim_binding.node`)
25
+ console.info("Copying libzim.8.dylib to build folder");
26
+ exec("cp download/lib/libzim.8.dylib build/Release/libzim.8.dylib");
27
+ exec("ln -sf build/Release/libzim.8.dylib build/Release/libzim.dylib"); // convienience only, not required
28
+ console.info("Fixing rpath");
29
+ exec(
30
+ "install_name_tool -change libzim.8.dylib @loader_path/libzim.8.dylib build/Release/zim_binding.node"
31
+ );
26
32
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,254 @@
1
- import { ZimArticle } from './zim';
2
- import { ZimReader } from './ZimReader';
3
- import { ZimCreator } from './ZimCreator';
4
- export { ZimArticle, ZimCreator, ZimReader, };
1
+ export class IntegrityCheck {
2
+ static CHECKSUM: symbol;
3
+ static DIRENT_PTRS: symbol;
4
+ static DIRENT_ORDER: symbol;
5
+ static TITLE_INDEX: symbol;
6
+ static CLUSTER_PTRS: symbol;
7
+ static DIRENT_MIMETYPES: symbol;
8
+ static COUNT: symbol; // DO NOT USE THIS. See libzim docs.
9
+ }
10
+
11
+ export class Compression {
12
+ static None: symbol;
13
+ static Zstd: symbol;
14
+ }
15
+
16
+ export class Blob {
17
+ constructor(buf?: ArrayBuffer | Buffer | string);
18
+ get data(): Buffer;
19
+ get size(): number | bigint;
20
+ toString(): string;
21
+ }
22
+
23
+ export type ContentProvider = {
24
+ size: number | bigint;
25
+ feed(): Blob;
26
+ };
27
+
28
+ export class StringProvider {
29
+ constructor(content: string);
30
+ get size(): number;
31
+ feed(): Blob;
32
+ }
33
+
34
+ export class FileProvider {
35
+ constructor(filepath: string);
36
+ get size(): number | bigint;
37
+ feed(): Blob;
38
+ }
39
+
40
+ export type Hint = {
41
+ COMPRESS?: number;
42
+ FRONT_ARTICLE?: number;
43
+ };
44
+
45
+ export interface IndexData {
46
+ hasIndexData?: boolean;
47
+ title?: string;
48
+ content?: string;
49
+ keywords?: string;
50
+ wordcount?: number;
51
+ position?: [boolean, number, number];
52
+ }
53
+
54
+ export interface WriterItem {
55
+ path: string;
56
+ title: string;
57
+ mimeType: string;
58
+ getContentProvider(): ContentProvider;
59
+ hints: Hint;
60
+ getIndexData?: () => IndexData;
61
+ }
62
+
63
+ export class StringItem {
64
+ constructor(
65
+ path: string,
66
+ mimeType: string,
67
+ title: string,
68
+ hint: Hint,
69
+ content: ArrayBuffer | Buffer | string
70
+ );
71
+ readonly path: string;
72
+ readonly title: string;
73
+ readonly mimeType: string;
74
+ getContentProvider(): StringProvider;
75
+ readonly hints: Hint;
76
+ }
77
+
78
+ export class FileItem {
79
+ constructor(
80
+ path: string,
81
+ mimeType: string,
82
+ title: string,
83
+ hints: Hint,
84
+ filePath: string
85
+ );
86
+ readonly path: string;
87
+ readonly title: string;
88
+ readonly mimeType: string;
89
+ getContentProvider(): StringProvider;
90
+ readonly hints: Hint;
91
+ }
92
+
93
+ export class Creator {
94
+ constructor();
95
+ configVerbose(verbose: boolean): this;
96
+ configCompression(value: Compression): this;
97
+ configClusterSize(size: number): this;
98
+ configIndexing(indexing: boolean, language: string): this;
99
+ configNbWorkers(num: number): this;
100
+ startZimCreation(filepath: string): this;
101
+ addItem(item: WriterItem): Promise<void>;
102
+ addMetadata(
103
+ name: string,
104
+ content: string | ContentProvider,
105
+ mimetype?: string
106
+ ): void;
107
+ addIllustration(size: number, content: string | ContentProvider): void;
108
+ addRedirection(
109
+ path: string,
110
+ title: string,
111
+ targetPath: string,
112
+ hints?: Hint
113
+ ): void;
114
+ setMainPath(mainPath: string): void;
115
+ setUuid(uuid: string): void;
116
+ finishZimCreation(): Promise<void>;
117
+ }
118
+
119
+ export class Item {
120
+ get title(): string;
121
+ get path(): string;
122
+ get mimetype(): string;
123
+ get data(): Blob;
124
+ getData(offset?: number | bigint, limit?: number | bigint): Blob;
125
+ get size(): number | bigint;
126
+ get directAccessInformation(): {
127
+ filename: string;
128
+ offset: number;
129
+ };
130
+ get index(): number | bigint;
131
+ }
132
+
133
+ export class Entry {
134
+ get isRedirect(): boolean;
135
+ get title(): string;
136
+ get path(): string;
137
+ get item(): Item;
138
+ getItem(followRedirect?: boolean): Item;
139
+ get redirect(): Item;
140
+ get redirectEntry(): Entry;
141
+ get index(): number;
142
+ }
143
+
144
+ export interface EntryRange extends Iterable<Entry> {
145
+ size: number;
146
+ offset(start: number, maxResults: number): EntryRange;
147
+ }
148
+
149
+ export class Archive {
150
+ constructor(filepath: string);
151
+ get filename(): string;
152
+ get filesize(): number | bigint;
153
+ get allEntryCount(): number;
154
+ get entryCount(): number;
155
+ get articleCount(): number;
156
+ get uuid(): string;
157
+ getMetadata(name: string): string;
158
+ getMetadataItem(name: string): Item;
159
+ get metadataKeys(): string[];
160
+ getIllustrationItem(size: number): Item;
161
+ get illustrationSizes(): Set<number>;
162
+ getEntryByPath(path_or_idx: string | number): Entry;
163
+ getEntryByTitle(title_or_idx: string | number): Entry;
164
+ getEntryByClusterOrder(idx: number): Entry;
165
+ get mainEntry(): Entry;
166
+ get randomEntry(): Entry;
167
+ hasEntryByPath(path: string): boolean;
168
+ hasEntryByTitle(title: string): boolean;
169
+ hasMainEntry(): boolean;
170
+ hasIllustration(size: number): boolean;
171
+ hasFulltextIndex(): boolean;
172
+ hasTitleIndex(): boolean;
173
+ iterByPath(): EntryRange;
174
+ iterByTitle(): EntryRange;
175
+ iterEfficient(): EntryRange;
176
+ findByPath(path: string): EntryRange;
177
+ findByTitle(title: string): EntryRange;
178
+ get hasChecksum(): boolean;
179
+ get checksum(): string;
180
+ check(): boolean;
181
+ checkIntegrity(checkType: symbol): boolean; // one of IntegrityCheck
182
+ get isMultiPart(): boolean;
183
+ get hasNewNamespaceScheme(): boolean;
184
+
185
+ static validate(zimPath: string, checksToRun: symbol[]): boolean; // list of IntegrityCheck
186
+ }
187
+
188
+ interface Georange {
189
+ latitude: number;
190
+ longitude: number;
191
+ distance: number;
192
+ }
193
+
194
+ export class Query {
195
+ constructor(query: string);
196
+ setQuery(query: string): this;
197
+ setGeorange(latitude: number, longitude: number, distance: number): this;
198
+ get query(): string;
199
+ set query(query: string);
200
+ toString(): string;
201
+ get georange(): Georange;
202
+ set georange(range: Georange);
203
+ }
204
+
205
+ export class SearchIterator {
206
+ get path(): string;
207
+ get title(): string;
208
+ get score(): number;
209
+ get snippet(): string;
210
+ get wordCount(): number;
211
+ get size(): number;
212
+ get fileIndex(): number;
213
+ get zimId(): string;
214
+ get entry(): Entry;
215
+ }
216
+
217
+ export interface SearchResultSet extends Iterable<SearchIterator> {
218
+ readonly size: number;
219
+ }
220
+
221
+ export class Search {
222
+ getResults(start: number, maxResults: number): SearchResultSet;
223
+ get estimatedMatches(): number;
224
+ }
225
+
226
+ export class Searcher {
227
+ constructor(archives: Archive | Archive[]);
228
+ addArchive(archive: Archive): this;
229
+ search(query: string | Query): Search;
230
+ setVerbose(verbose: boolean): this;
231
+ }
232
+
233
+ export class SuggestionIterator {
234
+ get entry(): Entry;
235
+ get title(): string;
236
+ get path(): string;
237
+ get snippet(): string;
238
+ get hasSnippet(): boolean;
239
+ }
240
+
241
+ export interface SuggestionResultSet extends Iterable<SuggestionIterator> {
242
+ readonly size: number;
243
+ }
244
+
245
+ export class SuggestionSearch {
246
+ getResults(start: number, maxResults: number): SuggestionResultSet;
247
+ get estimatedMatches(): number;
248
+ }
249
+
250
+ export class SuggestionSearcher {
251
+ constructor(archives: Archive);
252
+ suggest(query: string): SuggestionSearch;
253
+ setVerbose(verbose: boolean): this;
254
+ }
package/dist/index.js CHANGED
@@ -1,9 +1,33 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZimReader = exports.ZimCreator = exports.ZimArticle = void 0;
4
- var zim_1 = require("./zim");
5
- Object.defineProperty(exports, "ZimArticle", { enumerable: true, get: function () { return zim_1.ZimArticle; } });
6
- var ZimReader_1 = require("./ZimReader");
7
- Object.defineProperty(exports, "ZimReader", { enumerable: true, get: function () { return ZimReader_1.ZimReader; } });
8
- var ZimCreator_1 = require("./ZimCreator");
9
- Object.defineProperty(exports, "ZimCreator", { enumerable: true, get: function () { return ZimCreator_1.ZimCreator; } });
1
+ import bindings from "bindings";
2
+
3
+ const {
4
+ Archive,
5
+ Entry,
6
+ IntegrityCheck,
7
+ Compression,
8
+ Blob,
9
+ Searcher,
10
+ Query,
11
+ SuggestionSearcher,
12
+ Creator,
13
+ StringProvider,
14
+ FileProvider,
15
+ StringItem,
16
+ FileItem,
17
+ } = bindings("zim_binding");
18
+
19
+ module.exports = {
20
+ Archive,
21
+ Entry,
22
+ IntegrityCheck,
23
+ Compression,
24
+ Blob,
25
+ Searcher,
26
+ Query,
27
+ SuggestionSearcher,
28
+ Creator,
29
+ StringProvider,
30
+ FileProvider,
31
+ StringItem,
32
+ FileItem,
33
+ };