@remix-run/cloudflare 1.3.3-pre.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.md +7 -0
- package/crypto.js +53 -0
- package/implementations.js +29 -0
- package/index.js +49 -0
- package/magicExports/esm/remix.js +11 -0
- package/magicExports/remix.js +38 -0
- package/package.json +24 -0
- package/sessions/cloudflareKVSessionStorage.js +74 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2021 Remix Software Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/crypto.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remix-run/cloudflare v1.3.3-pre.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
const sign = async (value, secret) => {
|
|
17
|
+
let key = await createKey(secret, ["sign"]);
|
|
18
|
+
let data = encoder.encode(value);
|
|
19
|
+
let signature = await crypto.subtle.sign("HMAC", key, data);
|
|
20
|
+
let hash = btoa(String.fromCharCode(...new Uint8Array(signature))).replace(/=+$/, "");
|
|
21
|
+
return value + "." + hash;
|
|
22
|
+
};
|
|
23
|
+
const unsign = async (signed, secret) => {
|
|
24
|
+
let index = signed.lastIndexOf(".");
|
|
25
|
+
let value = signed.slice(0, index);
|
|
26
|
+
let hash = signed.slice(index + 1);
|
|
27
|
+
let key = await createKey(secret, ["verify"]);
|
|
28
|
+
let data = encoder.encode(value);
|
|
29
|
+
let signature = byteStringToUint8Array(atob(hash));
|
|
30
|
+
let valid = await crypto.subtle.verify("HMAC", key, signature, data);
|
|
31
|
+
return valid ? value : false;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
async function createKey(secret, usages) {
|
|
35
|
+
let key = await crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
36
|
+
name: "HMAC",
|
|
37
|
+
hash: "SHA-256"
|
|
38
|
+
}, false, usages);
|
|
39
|
+
return key;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function byteStringToUint8Array(byteString) {
|
|
43
|
+
let array = new Uint8Array(byteString.length);
|
|
44
|
+
|
|
45
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
46
|
+
array[i] = byteString.charCodeAt(i);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return array;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.sign = sign;
|
|
53
|
+
exports.unsign = unsign;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remix-run/cloudflare v1.3.3-pre.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var serverRuntime = require('@remix-run/server-runtime');
|
|
16
|
+
var crypto = require('./crypto.js');
|
|
17
|
+
|
|
18
|
+
const createCookie = serverRuntime.createCookieFactory({
|
|
19
|
+
sign: crypto.sign,
|
|
20
|
+
unsign: crypto.unsign
|
|
21
|
+
});
|
|
22
|
+
const createCookieSessionStorage = serverRuntime.createCookieSessionStorageFactory(createCookie);
|
|
23
|
+
const createSessionStorage = serverRuntime.createSessionStorageFactory(createCookie);
|
|
24
|
+
const createMemorySessionStorage = serverRuntime.createMemorySessionStorageFactory(createSessionStorage);
|
|
25
|
+
|
|
26
|
+
exports.createCookie = createCookie;
|
|
27
|
+
exports.createCookieSessionStorage = createCookieSessionStorage;
|
|
28
|
+
exports.createMemorySessionStorage = createMemorySessionStorage;
|
|
29
|
+
exports.createSessionStorage = createSessionStorage;
|
package/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remix-run/cloudflare v1.3.3-pre.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var cloudflareKVSessionStorage = require('./sessions/cloudflareKVSessionStorage.js');
|
|
16
|
+
var implementations = require('./implementations.js');
|
|
17
|
+
var serverRuntime = require('@remix-run/server-runtime');
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
exports.createCloudflareKVSessionStorage = cloudflareKVSessionStorage.createCloudflareKVSessionStorage;
|
|
22
|
+
exports.createCookie = implementations.createCookie;
|
|
23
|
+
exports.createCookieSessionStorage = implementations.createCookieSessionStorage;
|
|
24
|
+
exports.createMemorySessionStorage = implementations.createMemorySessionStorage;
|
|
25
|
+
exports.createSessionStorage = implementations.createSessionStorage;
|
|
26
|
+
Object.defineProperty(exports, 'createRequestHandler', {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () { return serverRuntime.createRequestHandler; }
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(exports, 'createSession', {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () { return serverRuntime.createSession; }
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(exports, 'isCookie', {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: function () { return serverRuntime.isCookie; }
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(exports, 'isSession', {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () { return serverRuntime.isSession; }
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(exports, 'json', {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () { return serverRuntime.json; }
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(exports, 'redirect', {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get: function () { return serverRuntime.redirect; }
|
|
49
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remix-run/cloudflare v1.3.3-pre.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
export { createCloudflareKVSessionStorage, createCookie, createCookieSessionStorage, createMemorySessionStorage, createSessionStorage } from '@remix-run/cloudflare';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remix-run/cloudflare v1.3.3-pre.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var cloudflare = require('@remix-run/cloudflare');
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Object.defineProperty(exports, 'createCloudflareKVSessionStorage', {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return cloudflare.createCloudflareKVSessionStorage; }
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports, 'createCookie', {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return cloudflare.createCookie; }
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(exports, 'createCookieSessionStorage', {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return cloudflare.createCookieSessionStorage; }
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, 'createMemorySessionStorage', {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () { return cloudflare.createMemorySessionStorage; }
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(exports, 'createSessionStorage', {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return cloudflare.createSessionStorage; }
|
|
38
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@remix-run/cloudflare",
|
|
3
|
+
"description": "Cloudflare platform abstractions for Remix",
|
|
4
|
+
"version": "1.3.3-pre.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/remix-run/remix",
|
|
9
|
+
"directory": "packages/remix-cloudflare"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/remix-run/remix/issues"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@cloudflare/kv-asset-handler": "^0.1.3",
|
|
16
|
+
"@remix-run/server-runtime": "1.3.3-pre.0"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"@cloudflare/workers-types": "^2.2.2"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@cloudflare/workers-types": "^2.2.2"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @remix-run/cloudflare v1.3.3-pre.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) Remix Software Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
var implementations = require('../implementations.js');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a SessionStorage that stores session data in the Clouldflare KV Store.
|
|
19
|
+
*
|
|
20
|
+
* The advantage of using this instead of cookie session storage is that
|
|
21
|
+
* KV Store may contain much more data than cookies.
|
|
22
|
+
*/
|
|
23
|
+
function createCloudflareKVSessionStorage({
|
|
24
|
+
cookie,
|
|
25
|
+
kv
|
|
26
|
+
}) {
|
|
27
|
+
return implementations.createSessionStorage({
|
|
28
|
+
cookie,
|
|
29
|
+
|
|
30
|
+
async createData(data, expires) {
|
|
31
|
+
while (true) {
|
|
32
|
+
let randomBytes = new Uint8Array(8);
|
|
33
|
+
crypto.getRandomValues(randomBytes); // This storage manages an id space of 2^64 ids, which is far greater
|
|
34
|
+
// than the maximum number of files allowed on an NTFS or ext4 volume
|
|
35
|
+
// (2^32). However, the larger id space should help to avoid collisions
|
|
36
|
+
// with existing ids when creating new sessions, which speeds things up.
|
|
37
|
+
|
|
38
|
+
let id = [...randomBytes].map(x => x.toString(16).padStart(2, "0")).join("");
|
|
39
|
+
|
|
40
|
+
if (await kv.get(id, "json")) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await kv.put(id, JSON.stringify(data), {
|
|
45
|
+
expiration: expires ? Math.round(expires.getTime() / 1000) : undefined
|
|
46
|
+
});
|
|
47
|
+
return id;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
async readData(id) {
|
|
52
|
+
let session = await kv.get(id);
|
|
53
|
+
|
|
54
|
+
if (!session) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return JSON.parse(session);
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
async updateData(id, data, expires) {
|
|
62
|
+
await kv.put(id, JSON.stringify(data), {
|
|
63
|
+
expiration: expires ? Math.round(expires.getTime() / 1000) : undefined
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
async deleteData(id) {
|
|
68
|
+
await kv.delete(id);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
exports.createCloudflareKVSessionStorage = createCloudflareKVSessionStorage;
|