fake-current-time 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/browser.d.ts +7 -0
- package/dist/browser.js +32 -0
- package/dist/chunk-APIG4RGO.js +86 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +0 -0
- package/dist/node.d.ts +10 -0
- package/dist/node.js +25 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 oreshinya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# fake-current-time
|
|
2
|
+
|
|
3
|
+
<a href="https://github.com/oreshinya/fake-current-time/actions/workflows/check.yml"><img src="https://img.shields.io/github/actions/workflow/status/oreshinya/fake-current-time/check.yml?branch=main&logo=github&style=flat-square" /></a>
|
|
4
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-brightgreen.svg?style=flat-square" /></a>
|
|
5
|
+
<a href="https://npmjs.org/package/fake-current-time"><img src="https://img.shields.io/npm/v/fake-current-time.svg?style=flat-square" /></a>
|
|
6
|
+
|
|
7
|
+
Manipulate current time in your application for development and staging environments.
|
|
8
|
+
|
|
9
|
+
Uses `Proxy` and `AsyncLocalStorage` to isolate time contexts per request, allowing multiple users to test with different time offsets simultaneously.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install fake-current-time
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
This example shows integration with React Router framework mode. The key concept is:
|
|
20
|
+
|
|
21
|
+
- **Server**: Parse offset from cookie and wrap rendering with `runner.run(offset, ...)`
|
|
22
|
+
- **Client**: Call `setup()` to initialize time manipulation
|
|
23
|
+
- **UI**: Use `setOffset()` and `clearOffset()` to control time via cookie
|
|
24
|
+
|
|
25
|
+
### Server Entry (`entry.server.tsx`)
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import type { AppLoadContext, EntryContext } from "react-router";
|
|
29
|
+
import { setup, parseOffsetFromCookie } from "fake-current-time/node";
|
|
30
|
+
|
|
31
|
+
// Do NOT setup in production
|
|
32
|
+
const runner = process.env.STAGE !== "production" ? setup() : null;
|
|
33
|
+
|
|
34
|
+
export default function handleRequest(
|
|
35
|
+
request: Request,
|
|
36
|
+
responseStatusCode: number,
|
|
37
|
+
responseHeaders: Headers,
|
|
38
|
+
routerContext: EntryContext,
|
|
39
|
+
loadContext: AppLoadContext,
|
|
40
|
+
) {
|
|
41
|
+
const offset = runner
|
|
42
|
+
? parseOffsetFromCookie(request.headers.get("Cookie") || "")
|
|
43
|
+
: undefined;
|
|
44
|
+
|
|
45
|
+
const render = () => {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
// ...
|
|
48
|
+
const { pipe, abort } = renderToPipeableStream(/* ... */, {
|
|
49
|
+
// ...
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return offset && runner ? runner.run(offset, render) : render();
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Client Entry (`entry.client.tsx`)
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { startTransition } from "react";
|
|
62
|
+
import { setup } from "fake-current-time/browser";
|
|
63
|
+
|
|
64
|
+
// Do NOT setup in production
|
|
65
|
+
if (process.env.STAGE !== "production") {
|
|
66
|
+
setup();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
startTransition(() => {
|
|
70
|
+
// ...
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### UI Component
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { setOffset, clearOffset } from "fake-current-time/browser";
|
|
78
|
+
|
|
79
|
+
// Restrict access to this page in production (e.g., via routing or middleware)
|
|
80
|
+
export function TimeControl() {
|
|
81
|
+
return (
|
|
82
|
+
<div>
|
|
83
|
+
<p>Current: {new Date().toString()}</p>
|
|
84
|
+
{/* setOffset saves to cookie and reloads the page */}
|
|
85
|
+
<button onClick={() => setOffset({ days: 1 })}>+1 Day</button>
|
|
86
|
+
<button onClick={() => setOffset({ days: -1 })}>-1 Day</button>
|
|
87
|
+
<button onClick={() => setOffset({ months: 3, days: 7 })}>+3 Months +7 Days</button>
|
|
88
|
+
{/* clearOffset removes cookie and reloads the page */}
|
|
89
|
+
<button onClick={clearOffset}>Reset</button>
|
|
90
|
+
</div>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`setOffset` accepts: `years`, `months`, `days`, `hours`, `minutes`, `seconds`, `milliseconds`
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COOKIE_NAME,
|
|
3
|
+
encodeOffset,
|
|
4
|
+
parseOffsetFromCookie,
|
|
5
|
+
setupDateProxy
|
|
6
|
+
} from "./chunk-APIG4RGO.js";
|
|
7
|
+
|
|
8
|
+
// src/browser.ts
|
|
9
|
+
var initialized = false;
|
|
10
|
+
function setup() {
|
|
11
|
+
if (initialized) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
setupDateProxy(getOffset);
|
|
15
|
+
initialized = true;
|
|
16
|
+
}
|
|
17
|
+
function setOffset(offset) {
|
|
18
|
+
document.cookie = `${COOKIE_NAME}=${encodeOffset(offset)}; path=/`;
|
|
19
|
+
location.reload();
|
|
20
|
+
}
|
|
21
|
+
function clearOffset() {
|
|
22
|
+
document.cookie = `${COOKIE_NAME}=; path=/; max-age=0`;
|
|
23
|
+
location.reload();
|
|
24
|
+
}
|
|
25
|
+
function getOffset() {
|
|
26
|
+
return parseOffsetFromCookie(document.cookie);
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
clearOffset,
|
|
30
|
+
setOffset,
|
|
31
|
+
setup
|
|
32
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/cookie.ts
|
|
2
|
+
var COOKIE_NAME = "fake_current_time_offset";
|
|
3
|
+
function parseOffsetFromCookie(cookieString) {
|
|
4
|
+
if (!cookieString) {
|
|
5
|
+
return void 0;
|
|
6
|
+
}
|
|
7
|
+
const cookies = cookieString.split(";");
|
|
8
|
+
for (const cookie of cookies) {
|
|
9
|
+
const [name, value] = cookie.trim().split("=");
|
|
10
|
+
if (name === COOKIE_NAME && value) {
|
|
11
|
+
return decodeOffset(value);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return void 0;
|
|
15
|
+
}
|
|
16
|
+
function encodeOffset(offset) {
|
|
17
|
+
return JSON.stringify(offset);
|
|
18
|
+
}
|
|
19
|
+
function decodeOffset(value) {
|
|
20
|
+
return JSON.parse(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/core.ts
|
|
24
|
+
function setupDateProxy(getOffset) {
|
|
25
|
+
const OriginalDate = globalThis.Date;
|
|
26
|
+
const DateProxy = new Proxy(OriginalDate, {
|
|
27
|
+
construct(target, args, newTarget) {
|
|
28
|
+
const offset = getOffset();
|
|
29
|
+
if (offset && args.length === 0) {
|
|
30
|
+
const t = fakeCurrentTime(OriginalDate, offset);
|
|
31
|
+
return Reflect.construct(target, [t], newTarget);
|
|
32
|
+
}
|
|
33
|
+
return Reflect.construct(target, args, newTarget);
|
|
34
|
+
},
|
|
35
|
+
get(target, prop, receiver) {
|
|
36
|
+
if (prop === "now") {
|
|
37
|
+
const offset = getOffset();
|
|
38
|
+
if (offset) {
|
|
39
|
+
return () => fakeCurrentTime(OriginalDate, offset);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Reflect.get(target, prop, receiver);
|
|
43
|
+
},
|
|
44
|
+
apply(target, thisArg, args) {
|
|
45
|
+
const offset = getOffset();
|
|
46
|
+
if (offset && args.length === 0) {
|
|
47
|
+
const t = fakeCurrentTime(OriginalDate, offset);
|
|
48
|
+
return new OriginalDate(t).toString();
|
|
49
|
+
}
|
|
50
|
+
return Reflect.apply(target, thisArg, args);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
globalThis.Date = DateProxy;
|
|
54
|
+
}
|
|
55
|
+
function fakeCurrentTime(ctor, offset) {
|
|
56
|
+
const date = new ctor();
|
|
57
|
+
if (offset.years) {
|
|
58
|
+
date.setFullYear(date.getFullYear() + offset.years);
|
|
59
|
+
}
|
|
60
|
+
if (offset.months) {
|
|
61
|
+
date.setMonth(date.getMonth() + offset.months);
|
|
62
|
+
}
|
|
63
|
+
if (offset.days) {
|
|
64
|
+
date.setDate(date.getDate() + offset.days);
|
|
65
|
+
}
|
|
66
|
+
if (offset.hours) {
|
|
67
|
+
date.setHours(date.getHours() + offset.hours);
|
|
68
|
+
}
|
|
69
|
+
if (offset.minutes) {
|
|
70
|
+
date.setMinutes(date.getMinutes() + offset.minutes);
|
|
71
|
+
}
|
|
72
|
+
if (offset.seconds) {
|
|
73
|
+
date.setSeconds(date.getSeconds() + offset.seconds);
|
|
74
|
+
}
|
|
75
|
+
if (offset.milliseconds) {
|
|
76
|
+
date.setMilliseconds(date.getMilliseconds() + offset.milliseconds);
|
|
77
|
+
}
|
|
78
|
+
return date.getTime();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export {
|
|
82
|
+
COOKIE_NAME,
|
|
83
|
+
parseOffsetFromCookie,
|
|
84
|
+
encodeOffset,
|
|
85
|
+
setupDateProxy
|
|
86
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
File without changes
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { TimeOffset } from './index.js';
|
|
2
|
+
|
|
3
|
+
declare function parseOffsetFromCookie(cookieString: string | undefined): TimeOffset | undefined;
|
|
4
|
+
|
|
5
|
+
interface FakeTimeRunner {
|
|
6
|
+
run<T>(offset: TimeOffset, callback: () => T): T;
|
|
7
|
+
}
|
|
8
|
+
declare function setup(): FakeTimeRunner;
|
|
9
|
+
|
|
10
|
+
export { type FakeTimeRunner, parseOffsetFromCookie, setup };
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseOffsetFromCookie,
|
|
3
|
+
setupDateProxy
|
|
4
|
+
} from "./chunk-APIG4RGO.js";
|
|
5
|
+
|
|
6
|
+
// src/node.ts
|
|
7
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
8
|
+
var runner = null;
|
|
9
|
+
function setup() {
|
|
10
|
+
if (runner) {
|
|
11
|
+
return runner;
|
|
12
|
+
}
|
|
13
|
+
const storage = new AsyncLocalStorage();
|
|
14
|
+
setupDateProxy(() => storage.getStore());
|
|
15
|
+
runner = {
|
|
16
|
+
run(offset, callback) {
|
|
17
|
+
return storage.run(offset, callback);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
return runner;
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
parseOffsetFromCookie,
|
|
24
|
+
setup
|
|
25
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fake-current-time",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Manipulate current time in your application for development and staging environments",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"time",
|
|
7
|
+
"date",
|
|
8
|
+
"offset",
|
|
9
|
+
"manipulation",
|
|
10
|
+
"development",
|
|
11
|
+
"staging",
|
|
12
|
+
"simulation"
|
|
13
|
+
],
|
|
14
|
+
"author": "oreshinya",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": "oreshinya/fake-current-time",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./node": {
|
|
28
|
+
"types": "./dist/node.d.ts",
|
|
29
|
+
"default": "./dist/node.js"
|
|
30
|
+
},
|
|
31
|
+
"./browser": {
|
|
32
|
+
"types": "./dist/browser.d.ts",
|
|
33
|
+
"default": "./dist/browser.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@biomejs/biome": "2.3.7",
|
|
42
|
+
"@types/node": "^24.10.1",
|
|
43
|
+
"tsup": "^8.3.5",
|
|
44
|
+
"typescript": "^5.7.2",
|
|
45
|
+
"vitest": "^4.0.3"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"typecheck": "tsc",
|
|
49
|
+
"lint:ci": "biome ci ./src",
|
|
50
|
+
"lint:fix": "biome check --write ./src",
|
|
51
|
+
"test": "vitest run",
|
|
52
|
+
"build": "tsup"
|
|
53
|
+
}
|
|
54
|
+
}
|