@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 +1 -1
- package/.eslintignore +3 -0
- package/.eslintrc.js +39 -0
- package/Changelog +13 -0
- package/README.md +50 -28
- package/binding.gyp +3 -5
- package/bundle-libzim.js +22 -16
- package/dist/index.d.ts +254 -4
- package/dist/index.js +33 -9
- package/download-libzim.js +67 -48
- package/package.json +28 -24
- package/src/archive.h +494 -0
- package/src/blob.h +101 -0
- package/src/common.h +150 -0
- package/src/contentProvider.h +258 -0
- package/src/creator.h +345 -0
- package/src/entry.h +116 -0
- package/src/entryrange.h +106 -0
- package/src/index.d.ts +254 -0
- package/src/index.js +33 -0
- package/src/item.h +152 -0
- package/src/module.cc +42 -6
- package/src/search.h +527 -0
- package/src/suggestion.h +359 -0
- package/src/writerItem.h +462 -0
- package/tsconfig.json +4 -6
- package/dist/ZimCreator.d.ts +0 -34
- package/dist/ZimCreator.js +0 -177
- package/dist/ZimReader.d.ts +0 -13
- package/dist/ZimReader.js +0 -110
- package/dist/zim.d.ts +0 -2
- package/dist/zim.js +0 -11
- package/jest.config.js +0 -18
- package/src/article.cc +0 -228
- package/src/article.h +0 -114
- package/src/reader.cc +0 -110
- package/src/reader.h +0 -31
- package/src/writer.cc +0 -67
- package/src/writer.h +0 -38
package/dist/ZimReader.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare class ZimReader {
|
|
2
|
-
_reader: any;
|
|
3
|
-
constructor(fileName: string);
|
|
4
|
-
get isAlive(): any;
|
|
5
|
-
get fileName(): any;
|
|
6
|
-
destroy(): void;
|
|
7
|
-
getCountArticles(): Promise<any>;
|
|
8
|
-
getArticleById(articleId: number): Promise<any>;
|
|
9
|
-
getArticleByUrl(articleUrl: string): Promise<any>;
|
|
10
|
-
suggest(query: string): Promise<any>;
|
|
11
|
-
search(query: string): Promise<any>;
|
|
12
|
-
}
|
|
13
|
-
export { ZimReader };
|
package/dist/ZimReader.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.ZimReader = void 0;
|
|
40
|
-
var zim_1 = require("./zim");
|
|
41
|
-
var ZimReader = /** @class */ (function () {
|
|
42
|
-
function ZimReader(fileName) {
|
|
43
|
-
this._reader = new zim_1.ZimReaderWrapper(fileName);
|
|
44
|
-
}
|
|
45
|
-
Object.defineProperty(ZimReader.prototype, "isAlive", {
|
|
46
|
-
get: function () {
|
|
47
|
-
return !!this._reader && this._reader.isOpen();
|
|
48
|
-
},
|
|
49
|
-
enumerable: false,
|
|
50
|
-
configurable: true
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(ZimReader.prototype, "fileName", {
|
|
53
|
-
get: function () {
|
|
54
|
-
return this.isAlive ? this._reader.fileName : null;
|
|
55
|
-
},
|
|
56
|
-
enumerable: false,
|
|
57
|
-
configurable: true
|
|
58
|
-
});
|
|
59
|
-
ZimReader.prototype.destroy = function () {
|
|
60
|
-
this._reader.close();
|
|
61
|
-
this._reader = null;
|
|
62
|
-
};
|
|
63
|
-
ZimReader.prototype.getCountArticles = function () {
|
|
64
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
65
|
-
return __generator(this, function (_a) {
|
|
66
|
-
if (!this.isAlive)
|
|
67
|
-
throw new Error("This Reader has been destroyed");
|
|
68
|
-
return [2 /*return*/, this._reader.getCountArticles()];
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
ZimReader.prototype.getArticleById = function (articleId) {
|
|
73
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
74
|
-
return __generator(this, function (_a) {
|
|
75
|
-
if (!this.isAlive)
|
|
76
|
-
throw new Error("This Reader has been destroyed");
|
|
77
|
-
return [2 /*return*/, this._reader.getArticleById(articleId)];
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
ZimReader.prototype.getArticleByUrl = function (articleUrl) {
|
|
82
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
83
|
-
return __generator(this, function (_a) {
|
|
84
|
-
if (!this.isAlive)
|
|
85
|
-
throw new Error("This Reader has been destroyed");
|
|
86
|
-
return [2 /*return*/, this._reader.getArticleByUrl(articleUrl)];
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
ZimReader.prototype.suggest = function (query) {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
92
|
-
return __generator(this, function (_a) {
|
|
93
|
-
if (!this.isAlive)
|
|
94
|
-
throw new Error("This Reader has been destroyed");
|
|
95
|
-
return [2 /*return*/, this._reader.suggest(query)];
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
ZimReader.prototype.search = function (query) {
|
|
100
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
101
|
-
return __generator(this, function (_a) {
|
|
102
|
-
if (!this.isAlive)
|
|
103
|
-
throw new Error("This Reader has been destroyed");
|
|
104
|
-
return [2 /*return*/, this._reader.search(query)];
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
return ZimReader;
|
|
109
|
-
}());
|
|
110
|
-
exports.ZimReader = ZimReader;
|
package/dist/zim.d.ts
DELETED
package/dist/zim.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ZimReaderWrapper = exports.ZimCreatorWrapper = exports.ZimArticle = void 0;
|
|
7
|
-
var bindings_1 = __importDefault(require("bindings"));
|
|
8
|
-
var _a = bindings_1.default('zim_binding'), ZimArticle = _a.Article, ZimCreatorWrapper = _a.ZimCreatorWrapper, ZimReaderWrapper = _a.ZimReaderWrapper;
|
|
9
|
-
exports.ZimArticle = ZimArticle;
|
|
10
|
-
exports.ZimCreatorWrapper = ZimCreatorWrapper;
|
|
11
|
-
exports.ZimReaderWrapper = ZimReaderWrapper;
|
package/jest.config.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
transform: {
|
|
3
|
-
"^.+\\.tsx?$": "ts-jest",
|
|
4
|
-
},
|
|
5
|
-
testMatch: ["**/test/*.test.ts"],
|
|
6
|
-
moduleFileExtensions: ["ts", "js"],
|
|
7
|
-
collectCoverage: false,
|
|
8
|
-
preset: 'ts-jest/presets/js-with-ts',
|
|
9
|
-
testEnvironment: 'node',
|
|
10
|
-
globals: {
|
|
11
|
-
'ts-jest': {
|
|
12
|
-
tsConfig: {
|
|
13
|
-
sourceMap: true,
|
|
14
|
-
inlineSourceMap: true
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
package/src/article.cc
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
#include "article.h"
|
|
3
|
-
|
|
4
|
-
#include <napi.h>
|
|
5
|
-
#include <zim/article.h>
|
|
6
|
-
#include <zim/blob.h>
|
|
7
|
-
#include <zim/writer/creator.h>
|
|
8
|
-
|
|
9
|
-
#include <cstring>
|
|
10
|
-
#include <memory>
|
|
11
|
-
#include <string>
|
|
12
|
-
#include <utility>
|
|
13
|
-
#include <vector>
|
|
14
|
-
|
|
15
|
-
Napi::FunctionReference Article::constructor;
|
|
16
|
-
|
|
17
|
-
Napi::Object Article::New(Napi::Env env, zim::Article* article) {
|
|
18
|
-
Napi::HandleScope scope(env);
|
|
19
|
-
if (article == nullptr) {
|
|
20
|
-
throw Napi::Error::New(env, "article is null");
|
|
21
|
-
}
|
|
22
|
-
if (!article->good()) {
|
|
23
|
-
throw Napi::Error::New(env, "article is not good");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
auto external = Napi::External<zim::Article>::New(env, article);
|
|
27
|
-
return constructor.New({external});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
Article::Article(const Napi::CallbackInfo& info)
|
|
31
|
-
: Napi::ObjectWrap<Article>(info) {
|
|
32
|
-
Napi::Env env = info.Env();
|
|
33
|
-
Napi::HandleScope scope(env);
|
|
34
|
-
|
|
35
|
-
if (!(info[0].IsObject() || info[0].IsExternal())) {
|
|
36
|
-
throw Napi::Error::New(
|
|
37
|
-
env, "1st argument must be an object with properies for Article");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (info[0].IsExternal()) {
|
|
41
|
-
// parse from external
|
|
42
|
-
auto article = reinterpret_cast<zim::Article*>(
|
|
43
|
-
info[0].As<Napi::External<zim::Article>>().Data());
|
|
44
|
-
if (!article->good()) {
|
|
45
|
-
throw Napi::Error::New(info.Env(),
|
|
46
|
-
"Invalid article, article is not 'good'.");
|
|
47
|
-
}
|
|
48
|
-
auto url = article->getUrl();
|
|
49
|
-
auto ns = std::string(1, article->getNamespace())[0];
|
|
50
|
-
auto aid = std::string(1, ns) + "/" + url;
|
|
51
|
-
auto mimeType = !article->isRedirect() ? article->getMimeType() : "";
|
|
52
|
-
auto redirectUrl =
|
|
53
|
-
article->isRedirect() ? article->getRedirectArticle().getLongUrl() : "";
|
|
54
|
-
article_ = std::make_shared<ZimArticle>(ns, aid, url, article->getTitle(),
|
|
55
|
-
mimeType, redirectUrl, "", false);
|
|
56
|
-
setData(article->getData());
|
|
57
|
-
} else {
|
|
58
|
-
// parse from object
|
|
59
|
-
auto props = info[0].ToObject();
|
|
60
|
-
|
|
61
|
-
std::string url = props.Get("url").ToString();
|
|
62
|
-
|
|
63
|
-
char ns = '.';
|
|
64
|
-
if (props.Has("ns")) {
|
|
65
|
-
std::string str = props.Get("ns").ToString();
|
|
66
|
-
if (str.length() != 1) {
|
|
67
|
-
throw Napi::Error::New(env, "ns must be exactly one character");
|
|
68
|
-
}
|
|
69
|
-
ns = str[0];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
std::string mimeType = props.Has("mimeType")
|
|
73
|
-
? std::string(props.Get("mimeType").ToString())
|
|
74
|
-
: "";
|
|
75
|
-
std::string title =
|
|
76
|
-
props.Has("title") ? std::string(props.Get("title").ToString()) : "";
|
|
77
|
-
std::string redirectUrl =
|
|
78
|
-
props.Has("redirectUrl")
|
|
79
|
-
? std::string(props.Get("redirectUrl").ToString())
|
|
80
|
-
: "";
|
|
81
|
-
std::string fileName = props.Has("fileName")
|
|
82
|
-
? std::string(props.Get("fileName").ToString())
|
|
83
|
-
: "";
|
|
84
|
-
bool shouldIndex =
|
|
85
|
-
props.Has("shouldIndex")
|
|
86
|
-
? static_cast<bool>(props.Get("shouldIndex").ToBoolean())
|
|
87
|
-
: false;
|
|
88
|
-
|
|
89
|
-
// `${props.ns}/${props.url}`
|
|
90
|
-
std::string aid = props.Has("aid") ? props.Get("aid").ToString()
|
|
91
|
-
: (std::string(1, ns) + "/" + url);
|
|
92
|
-
|
|
93
|
-
article_ = std::make_shared<ZimArticle>(ns, aid, url, title, mimeType,
|
|
94
|
-
redirectUrl, fileName, shouldIndex);
|
|
95
|
-
setData(info, props.Get("data"));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
Napi::Value Article::getNs(const Napi::CallbackInfo& info) {
|
|
100
|
-
return Napi::String::New(info.Env(), std::string(1, article_->ns));
|
|
101
|
-
}
|
|
102
|
-
void Article::setNs(const Napi::CallbackInfo& info, const Napi::Value& value) {
|
|
103
|
-
std::string str = value.ToString();
|
|
104
|
-
if (str.length() != 1) {
|
|
105
|
-
throw Napi::Error::New(info.Env(), "ns must be exactly one character");
|
|
106
|
-
}
|
|
107
|
-
article_->ns = str[0];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
Napi::Value Article::getAid(const Napi::CallbackInfo& info) {
|
|
111
|
-
return Napi::String::New(info.Env(), article_->aid);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
void Article::setAid(const Napi::CallbackInfo& info, const Napi::Value& value) {
|
|
115
|
-
article_->aid = value.ToString();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
Napi::Value Article::getUrl(const Napi::CallbackInfo& info) {
|
|
119
|
-
return Napi::String::New(info.Env(), article_->url);
|
|
120
|
-
}
|
|
121
|
-
void Article::setUrl(const Napi::CallbackInfo& info, const Napi::Value& value) {
|
|
122
|
-
article_->url = value.ToString();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
Napi::Value Article::getTitle(const Napi::CallbackInfo& info) {
|
|
126
|
-
return Napi::String::New(info.Env(), article_->title);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
void Article::setTitle(const Napi::CallbackInfo& info,
|
|
130
|
-
const Napi::Value& value) {
|
|
131
|
-
article_->title = value.ToString();
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
Napi::Value Article::getMimeType(const Napi::CallbackInfo& info) {
|
|
135
|
-
return Napi::String::New(info.Env(), article_->mimeType);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
void Article::setMimeType(const Napi::CallbackInfo& info,
|
|
139
|
-
const Napi::Value& value) {
|
|
140
|
-
article_->mimeType = value.ToString();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
Napi::Value Article::getRedirectAid(const Napi::CallbackInfo& info) {
|
|
144
|
-
return Napi::String::New(info.Env(), article_->redirectAid);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
void Article::setRedirectAid(const Napi::CallbackInfo& info,
|
|
148
|
-
const Napi::Value& value) {
|
|
149
|
-
article_->redirectAid = value.ToString();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
Napi::Value Article::getFilename(const Napi::CallbackInfo& info) {
|
|
153
|
-
return Napi::String::New(info.Env(), article_->getFilename());
|
|
154
|
-
}
|
|
155
|
-
void Article::setFilename(const Napi::CallbackInfo& info,
|
|
156
|
-
const Napi::Value& value) {
|
|
157
|
-
article_->fileName = value.ToString();
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
Napi::Value Article::getShouldIndex(const Napi::CallbackInfo& info) {
|
|
161
|
-
return Napi::Boolean::New(info.Env(), article_->shouldIndex());
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
void Article::setShouldIndex(const Napi::CallbackInfo& info,
|
|
165
|
-
const Napi::Value& value) {
|
|
166
|
-
article_->_shouldIndex = value.ToBoolean();
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
Napi::Value Article::getData(const Napi::CallbackInfo& info) {
|
|
170
|
-
return Napi::Buffer<uint8_t>::New(info.Env(), article_->bufferData.data(),
|
|
171
|
-
article_->bufferData.size());
|
|
172
|
-
}
|
|
173
|
-
void Article::setData(const Napi::CallbackInfo& info, const Napi::Value& data) {
|
|
174
|
-
if (data.IsString()) {
|
|
175
|
-
std::string nbuf = data.ToString();
|
|
176
|
-
article_->setData(nbuf.begin(), nbuf.end());
|
|
177
|
-
} else if (data.IsBuffer()) {
|
|
178
|
-
auto nbuf = data.As<Napi::Buffer<uint8_t>>();
|
|
179
|
-
article_->setData(nbuf.Data(), nbuf.Data() + nbuf.Length());
|
|
180
|
-
} else {
|
|
181
|
-
throw Napi::Error::New(info.Env(), "data must be a string or buffer");
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
void Article::setData(const zim::Blob& blob) {
|
|
186
|
-
article_->setData(blob.data(), blob.end());
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
Napi::Value Article::IsRedirect(const Napi::CallbackInfo& info) {
|
|
190
|
-
return Napi::Boolean::New(info.Env(), article_->isRedirect());
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
Napi::Value Article::getShouldCompress(const Napi::CallbackInfo& info) {
|
|
194
|
-
return Napi::Boolean::New(info.Env(), article_->shouldCompress());
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
void Article::Init(Napi::Env env, Napi::Object exports) {
|
|
198
|
-
Napi::HandleScope scope(env);
|
|
199
|
-
Napi::Function func = DefineClass(
|
|
200
|
-
env, "Article",
|
|
201
|
-
{
|
|
202
|
-
InstanceAccessor("ns", &Article::getNs, &Article::setNs),
|
|
203
|
-
InstanceAccessor("aid", &Article::getAid, &Article::setAid),
|
|
204
|
-
InstanceAccessor("url", &Article::getUrl, &Article::setUrl),
|
|
205
|
-
InstanceAccessor("title", &Article::getTitle, &Article::setTitle),
|
|
206
|
-
InstanceAccessor("mimeType", &Article::getMimeType,
|
|
207
|
-
&Article::setMimeType),
|
|
208
|
-
InstanceAccessor("redirectAid", &Article::getRedirectAid,
|
|
209
|
-
&Article::setRedirectAid),
|
|
210
|
-
InstanceAccessor("redirectUrl", &Article::getRedirectAid,
|
|
211
|
-
&Article::setRedirectAid),
|
|
212
|
-
InstanceAccessor("fileName", &Article::getFilename,
|
|
213
|
-
&Article::setFilename),
|
|
214
|
-
InstanceAccessor("shouldIndex", &Article::getShouldIndex,
|
|
215
|
-
&Article::setShouldIndex),
|
|
216
|
-
InstanceAccessor("data", &Article::getData, &Article::setData),
|
|
217
|
-
InstanceAccessor("bufferData", &Article::getData, &Article::setData),
|
|
218
|
-
InstanceMethod("isRedirect", &Article::IsRedirect),
|
|
219
|
-
InstanceAccessor("shouldCompress", &Article::getShouldCompress,
|
|
220
|
-
nullptr),
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
constructor = Napi::Persistent(func);
|
|
224
|
-
constructor.SuppressDestruct();
|
|
225
|
-
|
|
226
|
-
exports.Set("Article", func);
|
|
227
|
-
}
|
|
228
|
-
|
package/src/article.h
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <napi.h>
|
|
4
|
-
#include <zim/article.h>
|
|
5
|
-
#include <zim/blob.h>
|
|
6
|
-
#include <zim/writer/creator.h>
|
|
7
|
-
|
|
8
|
-
#include <memory>
|
|
9
|
-
#include <string>
|
|
10
|
-
#include <vector>
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* ZimArticle implementation
|
|
14
|
-
*/
|
|
15
|
-
class ZimArticle : public zim::writer::Article {
|
|
16
|
-
public:
|
|
17
|
-
ZimArticle(char ns, std::string aid, std::string url, std::string title,
|
|
18
|
-
std::string mimeType, std::string redirectAid,
|
|
19
|
-
std::string fileName, bool _shouldIndex)
|
|
20
|
-
: ns(ns),
|
|
21
|
-
aid(aid),
|
|
22
|
-
url(url),
|
|
23
|
-
title(title),
|
|
24
|
-
mimeType(mimeType),
|
|
25
|
-
redirectAid(redirectAid),
|
|
26
|
-
fileName(fileName),
|
|
27
|
-
_shouldIndex(_shouldIndex),
|
|
28
|
-
bufferData{} {}
|
|
29
|
-
|
|
30
|
-
char ns;
|
|
31
|
-
std::string aid;
|
|
32
|
-
std::string url;
|
|
33
|
-
std::string title;
|
|
34
|
-
std::string mimeType;
|
|
35
|
-
std::string redirectAid;
|
|
36
|
-
std::string fileName;
|
|
37
|
-
bool _shouldIndex;
|
|
38
|
-
std::vector<uint8_t> bufferData;
|
|
39
|
-
zim::Blob _data;
|
|
40
|
-
|
|
41
|
-
void setData(const std::vector<uint8_t> &data) {
|
|
42
|
-
bufferData = data;
|
|
43
|
-
_data = zim::Blob(reinterpret_cast<char *>(bufferData.data()),
|
|
44
|
-
bufferData.size());
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
template <class InputIt>
|
|
48
|
-
void setData(InputIt first, InputIt last) {
|
|
49
|
-
bufferData = std::vector<uint8_t>(first, last);
|
|
50
|
-
_data = zim::Blob(reinterpret_cast<char *>(bufferData.data()),
|
|
51
|
-
bufferData.size());
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
zim::Blob getData() const { return _data; }
|
|
55
|
-
std::string getFilename() const { return fileName; }
|
|
56
|
-
virtual zim::size_type getSize() const { return _data.size(); }
|
|
57
|
-
bool shouldIndex() const { return _shouldIndex; }
|
|
58
|
-
zim::writer::Url getUrl() const { return zim::writer::Url(ns, url); }
|
|
59
|
-
std::string getTitle() const { return title; }
|
|
60
|
-
bool isRedirect() const { return !redirectAid.empty(); }
|
|
61
|
-
std::string getMimeType() const { return mimeType; }
|
|
62
|
-
|
|
63
|
-
zim::writer::Url getRedirectUrl() const {
|
|
64
|
-
return zim::writer::Url(ns, redirectAid);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
bool shouldCompress() const {
|
|
68
|
-
return getMimeType().find("text") == 0 ||
|
|
69
|
-
getMimeType() == "application/javascript" ||
|
|
70
|
-
getMimeType() == "application/json" ||
|
|
71
|
-
getMimeType() == "image/svg+xml";
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Javascript Article binding object
|
|
77
|
-
*/
|
|
78
|
-
class Article : public Napi::ObjectWrap<Article> {
|
|
79
|
-
public:
|
|
80
|
-
static void Init(Napi::Env env, Napi::Object exports);
|
|
81
|
-
static Napi::Object New(Napi::Env env, zim::Article *article);
|
|
82
|
-
explicit Article(const Napi::CallbackInfo &info);
|
|
83
|
-
|
|
84
|
-
Napi::Value getNs(const Napi::CallbackInfo &info);
|
|
85
|
-
void setNs(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
86
|
-
Napi::Value getAid(const Napi::CallbackInfo &info);
|
|
87
|
-
void setAid(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
88
|
-
Napi::Value getUrl(const Napi::CallbackInfo &info);
|
|
89
|
-
void setUrl(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
90
|
-
Napi::Value getTitle(const Napi::CallbackInfo &info);
|
|
91
|
-
void setTitle(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
92
|
-
Napi::Value getMimeType(const Napi::CallbackInfo &info);
|
|
93
|
-
void setMimeType(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
94
|
-
Napi::Value getRedirectAid(const Napi::CallbackInfo &info);
|
|
95
|
-
void setRedirectAid(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
96
|
-
Napi::Value getFilename(const Napi::CallbackInfo &info);
|
|
97
|
-
void setFilename(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
98
|
-
Napi::Value getShouldIndex(const Napi::CallbackInfo &info);
|
|
99
|
-
void setShouldIndex(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
100
|
-
Napi::Value getData(const Napi::CallbackInfo &info);
|
|
101
|
-
void setData(const Napi::CallbackInfo &info, const Napi::Value &value);
|
|
102
|
-
|
|
103
|
-
void setData(const zim::Blob &blob);
|
|
104
|
-
|
|
105
|
-
Napi::Value IsRedirect(const Napi::CallbackInfo &info);
|
|
106
|
-
Napi::Value getShouldCompress(const Napi::CallbackInfo &info);
|
|
107
|
-
|
|
108
|
-
auto getArticle() { return article_; }
|
|
109
|
-
|
|
110
|
-
private:
|
|
111
|
-
static Napi::FunctionReference constructor;
|
|
112
|
-
std::shared_ptr<ZimArticle> article_;
|
|
113
|
-
};
|
|
114
|
-
|
package/src/reader.cc
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
#include "reader.h"
|
|
3
|
-
|
|
4
|
-
#include <napi.h>
|
|
5
|
-
#include <string>
|
|
6
|
-
|
|
7
|
-
Napi::FunctionReference ZimReaderWrapper::constructor;
|
|
8
|
-
|
|
9
|
-
ZimReaderWrapper::ZimReaderWrapper(const Napi::CallbackInfo& info)
|
|
10
|
-
: Napi::ObjectWrap<ZimReaderWrapper>(info) {
|
|
11
|
-
if (!info[0].IsString()) {
|
|
12
|
-
throw Napi::Error::New(info.Env(),
|
|
13
|
-
"first argument must be a string for fileName");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
fileName_ = info[0].ToString();
|
|
17
|
-
reader_ = std::make_shared<zim::File>(fileName_);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
void ZimReaderWrapper::close(const Napi::CallbackInfo& info) {
|
|
21
|
-
reader_ = nullptr;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
Napi::Value ZimReaderWrapper::isOpen(const Napi::CallbackInfo& info) {
|
|
25
|
-
return Napi::Boolean::New(info.Env(), reader_ != nullptr);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
Napi::Value ZimReaderWrapper::getCountArticles(const Napi::CallbackInfo& info) {
|
|
29
|
-
return Napi::Number::New(info.Env(), reader_->getCountArticles());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
Napi::Value ZimReaderWrapper::getArticleById(const Napi::CallbackInfo& info) {
|
|
33
|
-
zim::Article zarticle = reader_->getArticle(info[0].ToNumber().Uint32Value());
|
|
34
|
-
if (!zarticle.good()) {
|
|
35
|
-
throw Napi::Error::New(info.Env(), "Failed to find article");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return Article::New(info.Env(), &zarticle);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
Napi::Value ZimReaderWrapper::getArticleByUrl(const Napi::CallbackInfo& info) {
|
|
42
|
-
zim::Article zarticle = reader_->getArticleByUrl(info[0].ToString());
|
|
43
|
-
if (!zarticle.good()) {
|
|
44
|
-
throw Napi::Error::New(info.Env(), "Failed to find article");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return Article::New(info.Env(), &zarticle);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
Napi::Value ZimReaderWrapper::suggest(const Napi::CallbackInfo& info) {
|
|
51
|
-
auto env = info.Env();
|
|
52
|
-
Napi::HandleScope scope(env);
|
|
53
|
-
|
|
54
|
-
std::string query = info[0].ToString();
|
|
55
|
-
auto results = Napi::Array::New(env);
|
|
56
|
-
|
|
57
|
-
auto search = reader_->suggestions(query, 0, 10);
|
|
58
|
-
uint64_t idx = 0;
|
|
59
|
-
for (const auto& it : *search) {
|
|
60
|
-
results.Set(idx, Napi::String::New(env, it.getLongUrl()));
|
|
61
|
-
idx++;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return results;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
Napi::Value ZimReaderWrapper::search(const Napi::CallbackInfo& info) {
|
|
68
|
-
auto env = info.Env();
|
|
69
|
-
Napi::HandleScope scope(env);
|
|
70
|
-
|
|
71
|
-
std::string query = info[0].ToString();
|
|
72
|
-
auto results = Napi::Array::New(env);
|
|
73
|
-
|
|
74
|
-
auto search = reader_->search(query, 0, 10);
|
|
75
|
-
uint64_t idx = 0;
|
|
76
|
-
for (const auto& it : *search) {
|
|
77
|
-
results.Set(idx, Napi::String::New(env, it.getLongUrl()));
|
|
78
|
-
idx++;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return results;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
Napi::Value ZimReaderWrapper::getFilename(const Napi::CallbackInfo& info) {
|
|
85
|
-
return Napi::String::New(info.Env(), reader_->getFilename());
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
void ZimReaderWrapper::Init(Napi::Env env, Napi::Object exports) {
|
|
89
|
-
Napi::HandleScope scope(env);
|
|
90
|
-
Napi::Function func = DefineClass(
|
|
91
|
-
env, "ZimReaderWrapper",
|
|
92
|
-
{
|
|
93
|
-
InstanceMethod("close", &ZimReaderWrapper::close),
|
|
94
|
-
InstanceMethod("isOpen", &ZimReaderWrapper::isOpen),
|
|
95
|
-
InstanceMethod("getCountArticles",
|
|
96
|
-
&ZimReaderWrapper::getCountArticles),
|
|
97
|
-
InstanceMethod("getArticleById", &ZimReaderWrapper::getArticleById),
|
|
98
|
-
InstanceMethod("getArticleByUrl", &ZimReaderWrapper::getArticleByUrl),
|
|
99
|
-
InstanceMethod("getFilename", &ZimReaderWrapper::getFilename),
|
|
100
|
-
InstanceMethod("suggest", &ZimReaderWrapper::suggest),
|
|
101
|
-
InstanceMethod("search", &ZimReaderWrapper::search),
|
|
102
|
-
InstanceAccessor("fileName", &ZimReaderWrapper::getFilename, nullptr),
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
constructor = Napi::Persistent(func);
|
|
106
|
-
constructor.SuppressDestruct();
|
|
107
|
-
|
|
108
|
-
exports.Set("ZimReaderWrapper", func);
|
|
109
|
-
}
|
|
110
|
-
|
package/src/reader.h
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
#include <napi.h>
|
|
3
|
-
#include <zim/file.h>
|
|
4
|
-
#include <zim/search.h>
|
|
5
|
-
|
|
6
|
-
#include <memory>
|
|
7
|
-
#include <string>
|
|
8
|
-
|
|
9
|
-
#include "article.h"
|
|
10
|
-
|
|
11
|
-
class ZimReaderWrapper : public Napi::ObjectWrap<ZimReaderWrapper> {
|
|
12
|
-
public:
|
|
13
|
-
static void Init(Napi::Env env, Napi::Object exports);
|
|
14
|
-
explicit ZimReaderWrapper(const Napi::CallbackInfo& info);
|
|
15
|
-
|
|
16
|
-
void close(const Napi::CallbackInfo& info);
|
|
17
|
-
Napi::Value isOpen(const Napi::CallbackInfo& info);
|
|
18
|
-
|
|
19
|
-
Napi::Value getCountArticles(const Napi::CallbackInfo& info);
|
|
20
|
-
Napi::Value getArticleById(const Napi::CallbackInfo& info);
|
|
21
|
-
Napi::Value getArticleByUrl(const Napi::CallbackInfo& info);
|
|
22
|
-
Napi::Value suggest(const Napi::CallbackInfo& info);
|
|
23
|
-
Napi::Value search(const Napi::CallbackInfo& info);
|
|
24
|
-
|
|
25
|
-
Napi::Value getFilename(const Napi::CallbackInfo& info);
|
|
26
|
-
|
|
27
|
-
private:
|
|
28
|
-
static Napi::FunctionReference constructor;
|
|
29
|
-
std::shared_ptr<zim::File> reader_;
|
|
30
|
-
std::string fileName_;
|
|
31
|
-
};
|