Skip to content

Commit

Permalink
npm package save
Browse files Browse the repository at this point in the history
  • Loading branch information
9r3i committed Jan 15, 2023
1 parent 4703901 commit ffa9239
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 1 deletion.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@9r3i:registry=https://npm.pkg.github.com
10 changes: 10 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { WilayahClient } = require(__dirname+'/wilayah'),
path=process.argv[2]?process.argv[2]:'index',
host=process.argv[3]?process.argv[3]
:'https://9r3i.github.io/wilayah/api/2022/';

(async function(){
wilayah=new WilayahClient(host),
result=await wilayah.fetch(path);
console.log(result);
})();
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "wilayah",
"version": "1.3.0",
"description": "Module wilayah as client and server",
"main": "wilayah.js",
"scripts": {
"start": "node start.js",
"client": "node client.js",
"test": "node test.js"
},
"keywords": [
"wilayah",
"daerah",
"database",
"server",
"client",
"json"
],
"repository": {
"type": "git",
"url": "git+https://github.com/9r3i/wilayah.git"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com",
"access": "public"
},
"author": "9r3i",
"license": "MIT"
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Desa Baros, Arjasari, KAB. BANDUNG, JAWA BARAT

### Install
```html
<script src="https://9r3i.github.io/wilayah/wilayah.js"></script>
<script src="https://9r3i.github.io/wilayah/wilayah.client.js"></script>
```

### Usage
Expand Down
5 changes: 5 additions & 0 deletions start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { WilayahServer } = require(__dirname+'/wilayah'),
port=process.argv[2]&&process.argv[2].match(/^\d+$/)
?process.argv[2]:3000,
host='localhost',
wilserv=new WilayahServer(port,host);
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { WilayahClient } = require(__dirname+'/wilayah');

(async function(){
const host='https://9r3i.github.io/wilayah/api/2022/',
wilayah=new WilayahClient(host),
result=await wilayah.fetch('index');
console.log(result);
})();
19 changes: 19 additions & 0 deletions wilayah.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* WilayahClient */
function WilayahClient(host){
this.version='1.0.1';
this.host=host?host:'https://9r3i.github.io/wilayah/api/2022/';
this.fetch=function(path){
const _WilayahClient=this;
return new Promise(resolve=>{
return _WilayahClient.fetchCB(path,r=>{
return resolve(r);
});
});
};
this.fetchCB=function(path,cb){
cb=typeof cb==='function'?cb:function(){};
if(typeof path!=='string'){return cb(false);}
let url=this.host+path+'.json';
return fetch(url).then(r=>r.json()).then(cb);
};
}
65 changes: 65 additions & 0 deletions wilayah.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
const http = require("http");
const fs = require('fs').promises;

/* WilayahServer */
function WilayahServer(port,host){
const rl=function(req,res){
if(res.headersSent){return;}
headers(res);
/* checking options */
if(req.method=="OPTIONS"){
return options(res);
}
console.log(req.method,req.url);
const path=__dirname+'/api/2022'+req.url;
res.setHeader("Content-Type","application/json");
fs.access(path).then(r=>{
fs.readFile(path).then(con=>{
res.writeHead(200);
res.end(con);
}).catch(err => {
res.writeHead(500);
res.end(JSON.stringify({
code:500,
message:'Error: 500 - Failed to read data.',
}));
});
}).catch(err=>{
res.writeHead(404);
res.end(JSON.stringify({
code:404,
message:'Error: 404 - Data is not found.',
}));
});
},
serv=http.createServer(rl);
port=port?port:3000;
host=host?host:'localhost';
serv.listen(port,host,()=>{
console.log(`WilayahServer on http://${host}:${port}`);
});
/* options headers */
function options(res){
res.setHeader("Content-Language","en-US");
res.setHeader("Content-Encoding","gzip");
res.setHeader("Content-Length","0");
res.setHeader("Vary","Accept-Encoding, Origin");
res.writeHead(200);
res.end('');
}
/* default headers */
function headers(res){
/* access control - to allow the access via ajax */
res.setHeader("Access-Control-Allow-Origin","*"); // allow origin
res.setHeader("Access-Control-Request-Method","POST, GET, OPTIONS"); // request method
res.setHeader("Access-Control-Request-Headers","X-PINGOTHER, Content-Type"); // request header
res.setHeader("Access-Control-Max-Age","86400"); // max age (24 hours)
res.setHeader("Access-Control-Allow-Credentials","true"); // allow credentials
/* set content type of response header */
res.setHeader("Content-Type","text/plain;charset=utf-8;");
}
}

/* WilayahClient */
function WilayahClient(host){
this.version='1.0.1';
this.host=host?host:'https://9r3i.github.io/wilayah/api/2022/';
Expand All @@ -17,3 +79,6 @@ this.fetchCB=function(path,cb){
return fetch(url).then(r=>r.json()).then(cb);
};
}

exports.WilayahServer=WilayahServer;
exports.WilayahClient=WilayahClient;

0 comments on commit ffa9239

Please sign in to comment.