Skip to content

Commit 0c01167

Browse files
convert to typescript
1 parent a73150e commit 0c01167

14 files changed

+2602
-191
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ logs
1212
results
1313

1414
npm-debug.log
15+
dist
16+
node_modules/

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/index.ts",
12+
"preLaunchTask": "tsc: build - tsconfig.json",
13+
"outFiles": [
14+
"${workspaceFolder}/dist/**/*.js"
15+
]
16+
}
17+
]
18+
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,16 @@ Response should include something like this:
4747
##More Info
4848

4949
[Restful Web Service for PDF2JSON](http://www.codeproject.com/Articles/573297/Restful-Web-Service-for-PDF2JSON)
50+
51+
52+
# typescript
53+
54+
```
55+
npm install --save-dev typescript
56+
57+
npm i --save npmlog
58+
59+
npm install --save-dev @types/npmlog
60+
npm install --save-dev @types/restify
61+
npm i --save-dev @types/underscore
62+
```

index.js renamed to index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
//curl -isv -H "Content-Type: application/json" -X POST -d '{"folderName":"data", "pdfId":"xfa_1040"}' http://0.0.0.0:8001/p2jsvc
1010

1111
'use strict';
12-
var service = require("./lib/service");
13-
service.start();
12+
import {PDFFORMService} from './lib/service';
13+
(new PDFFORMService()).start();

lib/service.js

Lines changed: 0 additions & 121 deletions
This file was deleted.

lib/service.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
import log from "npmlog";
3+
import { plugins, pre, createServer, Response } from "restify";
4+
import { SvcContext } from './svccontext';
5+
import { SvcResponse } from './svcresponse';
6+
import PDFParser, { PDFDataReady, PDFFormImage, PDFParserError } from 'pdf2json';
7+
8+
9+
export class PDFFORMService {
10+
// private static
11+
private static _nextId = 1;
12+
private static _name = 'PDFFORMServer';
13+
private static _pdfPathBase = "";
14+
15+
get_id: () => number;
16+
get_name: () => string;
17+
get_version: () => string;
18+
19+
// constructor
20+
constructor() {
21+
// private, only accessible within this constructor
22+
let _id = PDFFORMService._nextId++;
23+
let _name = PDFFORMService._name;
24+
const _version = "0.0.1";
25+
26+
// public (every instance will have their own copy of these methods, needs to be lightweight)
27+
this.get_id = function() { return _id; };
28+
this.get_name = function() { return _name + _id; };
29+
this.get_version = function() {return _version; };
30+
};
31+
32+
// public static
33+
public static get_nextId() {
34+
return this._name + this._nextId;
35+
};
36+
37+
//private
38+
private _onPFBinDataReady(context: SvcContext , evtData: PDFFormImage) {
39+
log.info(this.get_name(), " completed response.");
40+
var resData = new SvcResponse(200, "OK", "data", evtData);
41+
context.completeResponse(resData);
42+
resData.destroy();
43+
evtData = null;
44+
return context.next();
45+
};
46+
47+
private _onPFBinDataError(context: SvcContext, error: PDFParserError) {
48+
log.info(this.get_name() + " 500 Error: ", JSON.stringify(error));
49+
var resData = new SvcResponse(500, JSON.stringify(error), undefined, undefined)
50+
context.completeResponse(resData);
51+
resData.destroy();
52+
resData = null;
53+
return context.next();
54+
};
55+
56+
_customizeHeaders(res: Response) {
57+
// Resitify currently has a bug which doesn't allow you to set default headers
58+
// This headers comply with CORS and allow us to server our response to any origin
59+
res.header("Access-Control-Allow-Origin", "*");
60+
res.header("Access-Control-Allow-Headers", "X-Requested-With");
61+
res.header("Cache-Control", "no-cache, must-revalidate");
62+
};
63+
64+
// public (every instance will share the same method, but has no access to private fields defined in constructor)
65+
start() {
66+
//private function within this public method
67+
const version = this.get_version();
68+
const _gfilter = (svcContext: SvcContext) => {
69+
var req = svcContext.request;
70+
var folderName = req.params.folderName;
71+
var pdfId = req.params.pdfId;
72+
log.info(this.get_name(), " received request:" + req.method + ":" + folderName + "/" + pdfId);
73+
74+
var pdfParser = new PDFParser();
75+
76+
this._customizeHeaders(svcContext.response);
77+
78+
pdfParser.on("pdfParser_dataReady", (data: PDFFormImage) => this._onPFBinDataReady(svcContext, data));
79+
pdfParser.on("pdfParser_dataError", (error: PDFParserError) => this._onPFBinDataError(svcContext, error));
80+
81+
pdfParser.loadPDF(PDFFORMService._pdfPathBase + folderName + "/" + pdfId + ".pdf", 0);
82+
};
83+
84+
const server = createServer({
85+
name: this.get_name(),
86+
version: this.get_version()
87+
});
88+
89+
server.use(plugins.acceptParser(server.acceptable));
90+
server.use(plugins.authorizationParser());
91+
server.use(plugins.dateParser());
92+
server.use(plugins.queryParser());
93+
server.use(plugins.bodyParser());
94+
server.use(plugins.jsonp());
95+
server.use(plugins.gzipResponse());
96+
server.pre(pre.userAgentConnection());
97+
98+
server.get('/p2jsvc/:folderName/:pdfId', function(req, res, next) {
99+
_gfilter(new SvcContext(req, res, next));
100+
});
101+
102+
server.post('/p2jsvc', function(req, res, next) {
103+
_gfilter(new SvcContext(req, res, next));
104+
});
105+
106+
server.get('/p2jsvc/status', function(req, res, next) {
107+
var jsObj = new SvcResponse(200, "OK", server.name, version);
108+
res.send(200, jsObj);
109+
return next();
110+
});
111+
112+
server.listen(7799, function() {
113+
log.info(server.name, ' listening at ' + server.url);
114+
});
115+
};
116+
};
117+
118+
119+

lib/svccontext.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/svccontext.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
import {Request, Response, Next} from 'restify';
3+
4+
export class SvcContext {
5+
request: Request;
6+
response: Response;
7+
next: Next;
8+
// constructor
9+
constructor(req: Request, res: Response, next: Next) {
10+
// public, this instance copies
11+
this.request = req;
12+
this.response = res;
13+
this.next = next;
14+
};
15+
16+
public completeResponse(jsObj) {
17+
this.response.send(200, jsObj);
18+
this.next();
19+
};
20+
21+
public destroy() {
22+
this.request = null;
23+
this.response = null;
24+
this.next = null;
25+
};
26+
}

lib/svcresponse.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)