node-window-polyfill
Advanced tools
Comparing version 1.0.0 to 1.0.2
@@ -0,0 +0,0 @@ # Contributor Covenant Code of Conduct |
19
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.register = exports.registerLocalStorage = exports.InMemoryLocalStorage = exports.registerWindow = exports.registerWindowProperties = exports.registerWebSocket = void 0; | ||
var globalObject = global; | ||
exports.registerWebSocket = function () { | ||
var registerWebSocket = function () { | ||
globalObject.WebSocket = globalObject.WebSocket || require("ws"); | ||
}; | ||
exports.registerWindowProperties = function () { | ||
exports.registerWebSocket = registerWebSocket; | ||
var registerWindowProperties = function () { | ||
globalObject.window.setTimeout = globalObject.setTimeout || setTimeout; | ||
@@ -16,7 +18,10 @@ globalObject.window.clearTimeout = globalObject.setTimeout || clearTimeout; | ||
globalObject.window.localStorage = globalObject.localStorage; | ||
globalObject.window.Date = globalObject.Date; | ||
}; | ||
exports.registerWindow = function () { | ||
exports.registerWindowProperties = registerWindowProperties; | ||
var registerWindow = function () { | ||
globalObject.window = globalObject.window || {}; | ||
exports.registerWindowProperties(); | ||
}; | ||
exports.registerWindow = registerWindow; | ||
var InMemoryLocalStorage = /** @class */ (function () { | ||
@@ -27,3 +32,3 @@ function InMemoryLocalStorage() { | ||
InMemoryLocalStorage.prototype.getItem = function (key) { | ||
return this.store[key]; | ||
return this.store[key] || null; | ||
}; | ||
@@ -41,5 +46,6 @@ ; | ||
exports.InMemoryLocalStorage = InMemoryLocalStorage; | ||
exports.registerLocalStorage = function () { | ||
var registerLocalStorage = function () { | ||
globalObject.localStorage = globalObject.localStorage || new InMemoryLocalStorage(); | ||
}; | ||
exports.registerLocalStorage = registerLocalStorage; | ||
/** | ||
@@ -53,3 +59,3 @@ * The function to polyfill with adding global.window. | ||
*/ | ||
exports.register = function (doPolyfillWebSockets) { | ||
var register = function (doPolyfillWebSockets) { | ||
if (doPolyfillWebSockets === void 0) { doPolyfillWebSockets = true; } | ||
@@ -62,3 +68,4 @@ if (doPolyfillWebSockets) { | ||
}; | ||
exports.register = register; | ||
exports.default = { register: exports.register }; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=model.js.map |
{ | ||
"name": "node-window-polyfill", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"description": "Polyfill for the problem `window is not defined` in node.js", | ||
@@ -32,6 +32,7 @@ "main": "index", | ||
"type": "git", | ||
"url": "git://github.com/tgorka/node-window-polyfill.git" | ||
"url": "git://github.com/tgorka/node-window-polyfill.git", | ||
"githubUsername": "tgorka" | ||
}, | ||
"engine": { | ||
"node": ">=6" | ||
"node": ">=14" | ||
}, | ||
@@ -41,13 +42,13 @@ "author": "Tomasz Gorka", | ||
"dependencies": { | ||
"ws": "^5.1.1" | ||
"ws": "^7.4.3" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^22.1.3", | ||
"@types/node": "^9.4.0", | ||
"debug": "^3.1.0", | ||
"jest": "^22.4.0", | ||
"ts-jest": "^22.0.4", | ||
"typescript": "^2.6.2", | ||
"yarn": "^1.3.2" | ||
"@types/jest": "^26.0.20", | ||
"@types/node": "^14.14.31", | ||
"debug": "^4.3.1", | ||
"jest": "^26.6.3", | ||
"ts-jest": "^26.5.3", | ||
"typescript": "^4.2.3", | ||
"yarn": "^1.22.10" | ||
} | ||
} |
@@ -66,2 +66,3 @@ # node-window-polyfill | ||
- `window.localStorage` - in memory storage | ||
- `window.Date` - version from global object | ||
- `global.WebSocket` - using [ws](https://www.npmjs.com/package/ws) if the right flag is not set | ||
@@ -68,0 +69,0 @@ - if the above functions/properties are already set in the `global` object then they will be taken into the account |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -5,3 +5,2 @@ import * as _debug from "debug"; | ||
const debug = _debug("nodeWindowPolyfill-test"); | ||
@@ -11,3 +10,3 @@ | ||
let item = null; | ||
let removedItem = null; | ||
let removedItem = true; | ||
@@ -18,5 +17,9 @@ beforeAll(async () => { | ||
nodeWindowPolyfill.register(); | ||
window.localStorage.setItem("TEST-1", 23); | ||
// @ts-ignore | ||
window.localStorage.setItem("TEST-1", "23"); | ||
// @ts-ignore | ||
item = window.localStorage.getItem("TEST-1"); | ||
// @ts-ignore | ||
window.localStorage.removeItem("TEST-1"); | ||
// @ts-ignore | ||
removedItem = window.localStorage.getItem("TEST-1"); | ||
@@ -30,14 +33,27 @@ debug(`after default polyfill is called.`); | ||
// @ts-ignore | ||
it("window should not throw an error", () => expect(() => {window.isNodeJS}).not.toThrowError()); | ||
// @ts-ignore | ||
it("window should be defined", () => expect(window).toBeDefined()); | ||
// @ts-ignore | ||
it("window.isNodeJS is true", () => expect(window.isNodeJS).toBe(true)); | ||
// @ts-ignore | ||
it("window.WebSocket should be instance of an object", () => expect(window.WebSocket).toBeDefined()); | ||
// @ts-ignore | ||
it("window.localStorage should be defined", () => expect(window.localStorage).toBeDefined()); | ||
it("window.localStorage should be instance of InMemoryStorage", () => expect(window.localStorage).toBeInstanceOf( | ||
InMemoryLocalStorage)); | ||
// Not anymore there is an localStorage implemented in node | ||
// @ts-ignore | ||
//it("window.localStorage should be instance of InMemoryStorage", () => expect(window.localStorage).toBeInstanceOf(InMemoryLocalStorage)); | ||
// @ts-ignore | ||
it("window.localStorage.getItem should be a function", () => expect(window.localStorage.getItem).toBeDefined()); | ||
// @ts-ignore | ||
it("window.localStorage.setItem should be a function", () => expect(window.localStorage.setItem).toBeDefined()); | ||
// @ts-ignore | ||
it("window.localStorage.removeItem should be a function", () => expect(window.localStorage.removeItem).toBeDefined()); | ||
it("window.localStorage.setItem and getItem works", () => expect(item).toBe(23)); | ||
it("window.localStorage.setItem, removeItem and getItem works", () => expect(removedItem).toBeUndefined()); | ||
it("window.localStorage.setItem and getItem works", () => expect(item).toBe("23")); | ||
it("window.localStorage.setItem, removeItem and getItem works", () => expect(removedItem).toBeNull()); | ||
// @ts-ignore | ||
it("window.Date should be instance of an object", () => expect(window.Date).toBeDefined()); | ||
// @ts-ignore | ||
it("window.Date.now should be a function", () => expect(window.Date.now).toBeDefined()); | ||
}); | ||
@@ -58,6 +74,10 @@ | ||
// @ts-ignore | ||
it("window should not throw an error", () => expect(() => {window.isNodeJS}).not.toThrowError()); | ||
// @ts-ignore | ||
it("window should be defined", () => expect(window).toBeDefined()); | ||
// @ts-ignore | ||
it("window.isNodeJS is true", () => expect(window.isNodeJS).toBe(true)); | ||
// @ts-ignore | ||
it("window.WebSocket should be undefined", () => expect(window.WebSocket).not.toBeDefined()); | ||
}); |
import { LocalStorage, WindowGlobal, Window } from "./model"; | ||
declare var window: Window; | ||
@@ -20,2 +19,3 @@ const globalObject: WindowGlobal = <any>global; | ||
globalObject.window.localStorage = globalObject.localStorage; | ||
globalObject.window.Date = globalObject.Date; | ||
}; | ||
@@ -29,9 +29,9 @@ | ||
export class InMemoryLocalStorage implements LocalStorage { | ||
private store: { [key:string]:any } = {}; | ||
private store: { [key:string]:string } = {}; | ||
public getItem(key: string): any { | ||
return this.store[key]; | ||
public getItem(key: string): string|null { | ||
return this.store[key] || null; | ||
}; | ||
public setItem(key: string, value: any): void { | ||
public setItem(key: string, value: string): void { | ||
this.store[key] = value; | ||
@@ -38,0 +38,0 @@ }; |
export interface LocalStorage { | ||
getItem(key: string): any, | ||
setItem(key: string, value: any): void, | ||
getItem(key: string): string|null, | ||
setItem(key: string, value: string): void, | ||
removeItem(key: string): void, | ||
@@ -18,2 +18,3 @@ } | ||
WebSocket: any, | ||
Date: any, | ||
} | ||
@@ -20,0 +21,0 @@ |
import { register } from "."; | ||
register(); |
@@ -0,0 +0,0 @@ { |
@@ -7,4 +7,4 @@ import { LocalStorage } from "./model"; | ||
private store; | ||
getItem(key: string): any; | ||
setItem(key: string, value: any): void; | ||
getItem(key: string): string | null; | ||
setItem(key: string, value: string): void; | ||
removeItem(key: string): void; | ||
@@ -11,0 +11,0 @@ } |
export interface LocalStorage { | ||
getItem(key: string): any; | ||
setItem(key: string, value: any): void; | ||
getItem(key: string): string | null; | ||
setItem(key: string, value: string): void; | ||
removeItem(key: string): void; | ||
@@ -17,2 +17,3 @@ } | ||
WebSocket: any; | ||
Date: any; | ||
} | ||
@@ -19,0 +20,0 @@ export interface WindowGlobal extends Window { |
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21573
288
121
+ Addedws@7.5.10(transitive)
- Removedasync-limiter@1.0.1(transitive)
- Removedws@5.2.4(transitive)
Updatedws@^7.4.3