raspi-serial-number
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -0,3 +1,4 @@ | ||
declare function getSerialNumber(callback: (error: any, data?: string) => void): void; | ||
declare function getSerialNumber(): Promise<string>; | ||
declare function getSerialNumberSync(): string; | ||
export { getSerialNumber, getSerialNumberSync }; |
@@ -1,79 +0,1 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
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; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs_1 = __importDefault(require("fs")); | ||
function filterSerialNumber(data) { | ||
var lines = data.split(/\r\n|\n/).filter(function (v) { return v.indexOf("Serial") === 0; }); | ||
if (lines.length <= 0) | ||
throw "Couldn't read the Serial number. Are you sure you're running this script on a Raspberry Pi?"; | ||
var line = lines[0]; | ||
var matches = line.match(/^Serial\s*:\s*([0-9a-f]+)$/i); | ||
if (!matches || matches.length <= 0) | ||
throw "Serial has invalid format. Are you sure you're running this script on a Raspberry Pi?"; | ||
return matches[1].replace(/^0+/, ''); | ||
} | ||
console.log(filterSerialNumber("\nprocessor\t: 0\nmodel name\t: ARMv6-compatible processor rev 7 (v6l)\nBogoMIPS\t: 997.08\nFeatures\t: half thumb fastmult vfp edsp java tls \nCPU implementer\t: 0x41\nCPU architecture: 7\nCPU variant\t: 0x0\nCPU part\t: 0xb76\nCPU revision\t: 7\n\nHardware\t: BCM2835\nRevision\t: 9000c1\nSerial\t\t: 00000000e02dd5bd\n")); | ||
function getSerialNumber() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2, new Promise(function (resolve, reject) { | ||
try { | ||
fs_1.default.readFile("/proc/cpuinfo", "ascii", function (err, data) { | ||
if (err) | ||
reject(err); | ||
else | ||
resolve(filterSerialNumber(data)); | ||
}); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
})]; | ||
}); | ||
}); | ||
} | ||
exports.getSerialNumber = getSerialNumber; | ||
function getSerialNumberSync() { | ||
var a = fs_1.default.readFileSync("/proc/cpuinfo", "ascii"); | ||
console.log(a); | ||
return filterSerialNumber(a); | ||
} | ||
exports.getSerialNumberSync = getSerialNumberSync; | ||
console.log(getSerialNumberSync()); | ||
"use strict";var __importDefault=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(exports,"__esModule",{value:!0});var fs_1=__importDefault(require("fs"));function isFunc(r){return!!r&&"[object Function]"==={}.toString.call(r)}function filterSerialNumber(r){var e=r.split(/\r\n|\n/).filter(function(r){return 0===r.indexOf("Serial")});if(e.length<=0)throw"Couldn't read the Serial number. Are you sure you're running this script on a Raspberry Pi?";var i=e[0].match(/^Serial\s*:\s*([0-9a-f]+)$/i);if(!i||i.length<=0)throw"Serial has invalid format. Are you sure you're running this script on a Raspberry Pi?";return i[1].replace(/^0+/,"")}function getSerialNumber(r){var e,i,t,n=void 0;r&&isFunc(r)?(e=function(e,i){try{var t=void 0;"string"==typeof i&&(t=filterSerialNumber(i)),r(e,t)}catch(e){r(e)}},n=void 0):(n=new Promise(function(r,e){i=r,t=e}),e=function(r,e){if(r)t(r);else try{i(void 0===e?void 0:filterSerialNumber(e))}catch(r){t(r)}});try{fs_1.default.readFile("/proc/cpuinfo","ascii",function(r,i){e(r,i)})}catch(r){e(r)}return n}function getSerialNumberSync(){return filterSerialNumber(fs_1.default.readFileSync("/proc/cpuinfo","ascii"))}exports.getSerialNumber=getSerialNumber,exports.getSerialNumberSync=getSerialNumberSync; |
74
index.ts
import fs from 'fs'; | ||
function isFunc<T>(o:T):T extends (...args: any[])=>any ? true : false { | ||
return (!!o && {}.toString.call(o) === '[object Function]') as any; | ||
} | ||
function filterSerialNumber(data: string): string { | ||
@@ -14,15 +18,51 @@ const lines = data.split(/\r\n|\n/).filter(v=>v.indexOf("Serial")===0); | ||
async function getSerialNumber(): Promise<string> { | ||
return new Promise((resolve,reject) => { | ||
try { | ||
fs.readFile("/proc/cpuinfo","ascii",(err,data)=>{ | ||
if(err) | ||
reject(err); | ||
else | ||
resolve(filterSerialNumber(data)); | ||
}) | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
function getSerialNumber(callback: (error: any,data?: string)=>void): void; | ||
async function getSerialNumber(): Promise<string>; | ||
function getSerialNumber(callback?: (error: any,data?: string)=>void): Promise<string>|void { | ||
let innerCallback: (error: any, data?: string) => (Promise<string>|void); | ||
let returnValue: Promise<string>|undefined = undefined; | ||
if(callback && isFunc(callback)) { | ||
innerCallback = (e,d)=>{ | ||
try { | ||
let a: string|undefined; | ||
if(typeof d === "string") | ||
a = filterSerialNumber(d); | ||
callback(e,a); | ||
} catch (e) { | ||
callback(e); | ||
} | ||
}; | ||
returnValue = undefined; | ||
} else { | ||
let resolve: (d: string | PromiseLike<string> | undefined)=>void; | ||
let reject: (e: any)=>void; | ||
returnValue = new Promise((res,rej)=>{ | ||
resolve = res; | ||
reject = rej; | ||
}); | ||
innerCallback = (e,d) => { | ||
if(e) | ||
reject(e); | ||
else { | ||
try { | ||
resolve(d===void 0 ? undefined : filterSerialNumber(d)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
}; | ||
} | ||
try { | ||
//https://raspberrypi.stackexchange.com/a/2087 | ||
fs.readFile("/proc/cpuinfo","ascii",(err,data)=>{ | ||
innerCallback(err,data); | ||
}) | ||
} catch (e) { | ||
innerCallback(e); | ||
} | ||
return returnValue; | ||
} | ||
@@ -34,2 +74,10 @@ | ||
/*getSerialNumber().then(d=>console.log("Promise result: ",d)).catch(e=>console.log("Promise error: ",e)); | ||
getSerialNumber((e,d)=>{if(e) console.error("Callback error: ",e); else console.log("Callback result: ",d)}); | ||
try { | ||
console.log("Sync result: ",getSerialNumberSync()); | ||
} catch (e) { | ||
console.error("Sync error: ",e); | ||
}*/ | ||
export {getSerialNumber, getSerialNumberSync}; |
{ | ||
"name": "raspi-serial-number", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Small library to get a Raspberry's Serial number without dependencies", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
7
1
32
13024
132
3