Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from terass-inc/fature/versionup-with-change-ex…
Browse files Browse the repository at this point in the history
…port

バージョン・パッケージ・exports更新
  • Loading branch information
koolii authored Jun 21, 2022
2 parents 0259c44 + 177dae3 commit 2b4927c
Show file tree
Hide file tree
Showing 11 changed files with 2,287 additions and 486 deletions.
37 changes: 37 additions & 0 deletions bin/exec-internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

const commandLineArgs = require("command-line-args");

const enrichmentAddress = require("../main");

const options = commandLineArgs([
{
name: "string",
alias: "s",
type: String,
typeLabel: "{underline string}",
description: "変換対象とする住所文字列",
},
]);

const sleep = (millisecond = 3000) =>
new Promise((resolve) => setTimeout(resolve, millisecond));

(async function main() {
console.info("Creating imi-enrichment-address instance");
await sleep();

const text = options.string;

if (typeof text !== "string") throw new Error("required string type");
if (text === "") throw new Error("required string values");

// const result = await instance.convert(text);
const result = await enrichmentAddress.convert(text);
console.info("Result", result);

console.info("Closing imi-enrichment-address instance");
await sleep();

enrichmentAddress.disconnect();
})();
67 changes: 36 additions & 31 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

const EnrichmentAddress = require("../main");
const enrichment = require("../main");
const http = require("http");

if (process.argv.length < 3 || !process.argv[2].match(/^[1-9][0-9]*$/)) {
Expand All @@ -9,66 +9,71 @@ if (process.argv.length < 3 || !process.argv[2].match(/^[1-9][0-9]*$/)) {
}

const port = parseInt(process.argv[2]);
const enrichment = new EnrichmentAddress();

const server = http.createServer((req, res) => {
if (req.method === "GET") {
res.writeHead(200, {
"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*"
"Access-Control-Allow-Origin": "*",
});
res.end(require('fs').readFileSync(__dirname + "/server.html", "utf-8"));
res.end(require("fs").readFileSync(__dirname + "/server.html", "utf-8"));
return;
}
if (req.method !== "POST") {
res.writeHead(405, {
"Content-Type": "text/plain",
"Allow": "POST",
"Access-Control-Allow-Origin": "*"
Allow: "POST",
"Access-Control-Allow-Origin": "*",
});
res.end("405 Method Not Allowed, only POST method is supported");
return;
}

const isJSON = req.headers["content-type"] && req.headers["content-type"].indexOf("json") !== -1;
const isJSON =
req.headers["content-type"] &&
req.headers["content-type"].indexOf("json") !== -1;

new Promise(resolve => {
new Promise((resolve) => {
let data = "";
req.on("data", (chunk) => {
data += chunk;
}).on("end", () => {
resolve(data);
});
}).then(data => {
req
.on("data", (chunk) => {
data += chunk;
})
.on("end", () => {
resolve(data);
});
}).then((data) => {
let input = data;
if (isJSON) {
try {
input = JSON.parse(data);
} catch (e) {
res.writeHead(400, {
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*"
"Access-Control-Allow-Origin": "*",
});
res.end(`400 Bad Request, exception occurred during parsing POST body as JSON\n\n${e.toString()}`);
res.end(
`400 Bad Request, exception occurred during parsing POST body as JSON\n\n${e.toString()}`
);
return;
}
}
enrichment.convert(input).then(json => {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(json, null, 2));

}).catch(e => {
res.writeHead(500, {
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*"
enrichment
.convert(input)
.then((json) => {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
});
res.end(JSON.stringify(json, null, 2));
})
.catch((e) => {
res.writeHead(500, {
"Content-Type": "text/plain",
"Access-Control-Allow-Origin": "*",
});
res.end(`500 Internal Server Error\n\n${e.toString()}`);
});
res.end(`500 Internal Server Error\n\n${e.toString()}`);
});
});

});
server.listen(port, () => {
console.log(`imi-enrichment-address-server is running on port ${port}`);
Expand Down
Loading

0 comments on commit 2b4927c

Please sign in to comment.