From 04eb28dda4c4a1dbd56637dc2006f8692fc30696 Mon Sep 17 00:00:00 2001 From: lidh04 Date: Sat, 21 Sep 2024 17:11:43 +0800 Subject: [PATCH 1/2] feat: support browser lib --- package-lock.json | 13 +++++++++++-- package.json | 2 +- src/circle-common.js | 16 +++++++++++++--- src/circle-user.js | 13 +++++++------ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33e1318..2ca87c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "@lidh04/circle-chain-sdk", - "version": "1.0.0", + "version": "1.0.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lidh04/circle-chain-sdk", - "version": "1.0.0", + "version": "1.0.7", "license": "ISC", "dependencies": { "axios": "^1.7.4", + "js-cookie": "^3.0.5", "tsd-jsdoc": "^2.5.0" }, "devDependencies": {} @@ -197,6 +198,14 @@ "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", "peer": true }, + "node_modules/js-cookie": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", + "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==", + "engines": { + "node": ">=14" + } + }, "node_modules/js2xmlparser": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", diff --git a/package.json b/package.json index dae2f35..6342d2e 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "license": "ISC", "dependencies": { "axios": "^1.7.4", + "js-cookie": "^3.0.5", "tsd-jsdoc": "^2.5.0" }, - "devDependencies": {}, "repository": { "type": "git", "url": "git@gitee.com:lidh04/js-circle-chain-sdk.git" diff --git a/src/circle-common.js b/src/circle-common.js index 0627450..9a54fdc 100644 --- a/src/circle-common.js +++ b/src/circle-common.js @@ -1,7 +1,12 @@ -import gateway from "./circle-gateway.js"; import axios from 'axios'; - import { readFile } from 'node:fs/promises'; +import Cookies from 'js-cookie'; +import gateway from "./circle-gateway.js"; + +/** + * @type string + */ +export const COOKIE_KEY = "userLoginInfo"; /** * build url tool @@ -69,7 +74,12 @@ export async function postData(url, input, timeout=30000) { */ export async function getSessionKey() { if (process.browser) { - // TODO return the data in cookie + const value = Cookies.get(COOKIE_KEY); + if (value) { + const userLoginInfo = JSON.parse(value); + const { sessionKey } = userLoginInfo; + return sessionKey; + } return null; } diff --git a/src/circle-user.js b/src/circle-user.js index 9971f9c..06574d8 100644 --- a/src/circle-user.js +++ b/src/circle-user.js @@ -5,11 +5,11 @@ * @license copyright to shc */ -import gateway from "./circle-gateway.js"; +import Cookies from 'js-cookie'; import { writeFile, mkdir } from 'node:fs/promises'; import path from 'path'; -import { buildUrl, getData, postData } from "./circle-common.js"; - +import { buildUrl, getData, postData, COOKIE_KEY } from "./circle-common.js"; +import gateway from "./circle-gateway.js"; /// @@ -137,7 +137,9 @@ export async function login(input) { */ export async function saveUserLoginInfo(userLoginInfo) { if (process.browser) { - // TODO store the login info in cookie + Cookies.set(COOKIE_KEY, JSON.stringify(userLoginInfo), { + expires: 7, path: '/' + }); return; } @@ -158,8 +160,7 @@ ${accountInfo} await mkdir(dir, { recursive: true }); await writeFile(sessionFullPath, content); } catch (err) { - console.error("cannot save user login info:", JSON.stringify(userLoginInfo), - "to session path:", sessionFullPath); + console.error("cannot save user login info:", JSON.stringify(userLoginInfo), "to session path:", sessionFullPath); } } -- Gitee From 67562ad61ccdc655849886796386a90102ae268b Mon Sep 17 00:00:00 2001 From: lidh04 Date: Sun, 22 Sep 2024 10:32:02 +0800 Subject: [PATCH 2/2] feat: add check isBrowser() --- src/circle-common.js | 23 ++++++++++++++++++++++- src/circle-user.js | 4 ++-- tools/logged-user.js | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/circle-common.js b/src/circle-common.js index 9a54fdc..18826bd 100644 --- a/src/circle-common.js +++ b/src/circle-common.js @@ -73,7 +73,7 @@ export async function postData(url, input, timeout=30000) { * get session key from the path. */ export async function getSessionKey() { - if (process.browser) { + if (isBrowser()) { const value = Cookies.get(COOKIE_KEY); if (value) { const userLoginInfo = JSON.parse(value); @@ -136,3 +136,24 @@ export async function getData(url, timeout=30000) { }; } } + +export function isBrowser() { + // Check if the environment is a + // Service worker + if (typeof importScripts === "function") { + return false; + } + + // Check if the environment is a Browser + if (typeof window === "object") { + return true; + } + + // Check if the environment is Node.js + if (typeof process === "object" + && typeof require === "function") { + return false; + } + + return false; +} diff --git a/src/circle-user.js b/src/circle-user.js index 06574d8..5bfd469 100644 --- a/src/circle-user.js +++ b/src/circle-user.js @@ -8,7 +8,7 @@ import Cookies from 'js-cookie'; import { writeFile, mkdir } from 'node:fs/promises'; import path from 'path'; -import { buildUrl, getData, postData, COOKIE_KEY } from "./circle-common.js"; +import { buildUrl, getData, postData, COOKIE_KEY, isBrowser } from "./circle-common.js"; import gateway from "./circle-gateway.js"; /// @@ -136,7 +136,7 @@ export async function login(input) { * @returns {Promise} - no returns. */ export async function saveUserLoginInfo(userLoginInfo) { - if (process.browser) { + if (isBrowser()) { Cookies.set(COOKIE_KEY, JSON.stringify(userLoginInfo), { expires: 7, path: '/' }); diff --git a/tools/logged-user.js b/tools/logged-user.js index dd5d9e9..63da7f4 100644 --- a/tools/logged-user.js +++ b/tools/logged-user.js @@ -9,6 +9,7 @@ async function main() { console.log("userInfo:", userInfo); const balanceOfWallet = await circle.wallet.getBalanceOfWallet(); console.log("balanceOfWallet:", balanceOfWallet); + console.log("require:", typeof(importScripts)); } async function loginUser() { -- Gitee