@pluv/platform-cloudflare 0.0.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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +17 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +89 -0
- package/dist/index.mjs +64 -0
- package/package.json +27 -0
- package/src/CloudflarePlatform.ts +24 -0
- package/src/CloudflareWebSocket.ts +64 -0
- package/src/index.ts +1 -0
- package/src/platformCloudflare.ts +5 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/platform-cloudflare@0.0.0 build /home/runner/work/pluv/pluv/packages/platform-cloudflare
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v6.5.0
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[34mCJS[39m Build start
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m1.50 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 80ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m2.52 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 82ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 3633ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.03 KB[39m
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPlatform } from '@pluv/io';
|
|
2
|
+
|
|
3
|
+
type CloudflareWebSocketConfig = AbstractWebSocketConfig;
|
|
4
|
+
declare class CloudflareWebSocket extends AbstractWebSocket {
|
|
5
|
+
webSocket: WebSocket;
|
|
6
|
+
get readyState(): 0 | 1 | 2 | 3;
|
|
7
|
+
constructor(webSocket: WebSocket, config: CloudflareWebSocketConfig);
|
|
8
|
+
addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
|
|
9
|
+
close(code?: number | undefined, reason?: string | undefined): void;
|
|
10
|
+
initialize(): Promise<() => undefined>;
|
|
11
|
+
send(message: string | ArrayBuffer | ArrayBufferView): void;
|
|
12
|
+
terminate(): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare class CloudflarePlatform extends AbstractPlatform<WebSocket> {
|
|
16
|
+
convertWebSocket(webSocket: WebSocket, config: AbstractWebSocketConfig): CloudflareWebSocket;
|
|
17
|
+
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
18
|
+
randomUUID(): string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare const platformCloudflare: () => CloudflarePlatform;
|
|
22
|
+
|
|
23
|
+
export { platformCloudflare };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
platformCloudflare: () => platformCloudflare
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/CloudflarePlatform.ts
|
|
28
|
+
var import_io2 = require("@pluv/io");
|
|
29
|
+
|
|
30
|
+
// src/CloudflareWebSocket.ts
|
|
31
|
+
var import_io = require("@pluv/io");
|
|
32
|
+
var CloudflareWebSocket = class extends import_io.AbstractWebSocket {
|
|
33
|
+
constructor(webSocket, config) {
|
|
34
|
+
const { room } = config;
|
|
35
|
+
super({ room });
|
|
36
|
+
this.webSocket = webSocket;
|
|
37
|
+
}
|
|
38
|
+
get readyState() {
|
|
39
|
+
return this.webSocket.readyState;
|
|
40
|
+
}
|
|
41
|
+
addEventListener(type, handler) {
|
|
42
|
+
this.webSocket.addEventListener(type, handler);
|
|
43
|
+
}
|
|
44
|
+
close(code, reason) {
|
|
45
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
46
|
+
(readyState) => readyState === this.readyState
|
|
47
|
+
);
|
|
48
|
+
if (!canClose)
|
|
49
|
+
return;
|
|
50
|
+
this.webSocket.close(code, reason);
|
|
51
|
+
}
|
|
52
|
+
initialize() {
|
|
53
|
+
this.webSocket.accept();
|
|
54
|
+
return Promise.resolve(() => void 0);
|
|
55
|
+
}
|
|
56
|
+
send(message) {
|
|
57
|
+
if (this.readyState !== this.OPEN)
|
|
58
|
+
return;
|
|
59
|
+
this.webSocket.send(message);
|
|
60
|
+
}
|
|
61
|
+
terminate() {
|
|
62
|
+
return this.webSocket.close(1011, "Terminated");
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/CloudflarePlatform.ts
|
|
67
|
+
var CloudflarePlatform = class extends import_io2.AbstractPlatform {
|
|
68
|
+
convertWebSocket(webSocket, config) {
|
|
69
|
+
return new CloudflareWebSocket(webSocket, config);
|
|
70
|
+
}
|
|
71
|
+
parseData(data) {
|
|
72
|
+
if (typeof data === "string")
|
|
73
|
+
return JSON.parse(data);
|
|
74
|
+
const decoder = new TextDecoder("utf8");
|
|
75
|
+
return JSON.parse(decoder.decode(data));
|
|
76
|
+
}
|
|
77
|
+
randomUUID() {
|
|
78
|
+
return crypto.randomUUID();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/platformCloudflare.ts
|
|
83
|
+
var platformCloudflare = () => {
|
|
84
|
+
return new CloudflarePlatform();
|
|
85
|
+
};
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
platformCloudflare
|
|
89
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/CloudflarePlatform.ts
|
|
2
|
+
import { AbstractPlatform } from "@pluv/io";
|
|
3
|
+
|
|
4
|
+
// src/CloudflareWebSocket.ts
|
|
5
|
+
import {
|
|
6
|
+
AbstractWebSocket
|
|
7
|
+
} from "@pluv/io";
|
|
8
|
+
var CloudflareWebSocket = class extends AbstractWebSocket {
|
|
9
|
+
constructor(webSocket, config) {
|
|
10
|
+
const { room } = config;
|
|
11
|
+
super({ room });
|
|
12
|
+
this.webSocket = webSocket;
|
|
13
|
+
}
|
|
14
|
+
get readyState() {
|
|
15
|
+
return this.webSocket.readyState;
|
|
16
|
+
}
|
|
17
|
+
addEventListener(type, handler) {
|
|
18
|
+
this.webSocket.addEventListener(type, handler);
|
|
19
|
+
}
|
|
20
|
+
close(code, reason) {
|
|
21
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
22
|
+
(readyState) => readyState === this.readyState
|
|
23
|
+
);
|
|
24
|
+
if (!canClose)
|
|
25
|
+
return;
|
|
26
|
+
this.webSocket.close(code, reason);
|
|
27
|
+
}
|
|
28
|
+
initialize() {
|
|
29
|
+
this.webSocket.accept();
|
|
30
|
+
return Promise.resolve(() => void 0);
|
|
31
|
+
}
|
|
32
|
+
send(message) {
|
|
33
|
+
if (this.readyState !== this.OPEN)
|
|
34
|
+
return;
|
|
35
|
+
this.webSocket.send(message);
|
|
36
|
+
}
|
|
37
|
+
terminate() {
|
|
38
|
+
return this.webSocket.close(1011, "Terminated");
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/CloudflarePlatform.ts
|
|
43
|
+
var CloudflarePlatform = class extends AbstractPlatform {
|
|
44
|
+
convertWebSocket(webSocket, config) {
|
|
45
|
+
return new CloudflareWebSocket(webSocket, config);
|
|
46
|
+
}
|
|
47
|
+
parseData(data) {
|
|
48
|
+
if (typeof data === "string")
|
|
49
|
+
return JSON.parse(data);
|
|
50
|
+
const decoder = new TextDecoder("utf8");
|
|
51
|
+
return JSON.parse(decoder.decode(data));
|
|
52
|
+
}
|
|
53
|
+
randomUUID() {
|
|
54
|
+
return crypto.randomUUID();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/platformCloudflare.ts
|
|
59
|
+
var platformCloudflare = () => {
|
|
60
|
+
return new CloudflarePlatform();
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
platformCloudflare
|
|
64
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/platform-cloudflare",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@pluv/io": "^0.0.0",
|
|
12
|
+
"@pluv/types": "^0.0.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@cloudflare/workers-types": "^3.18.0",
|
|
16
|
+
"@pluv/tsconfig": "^0.0.0",
|
|
17
|
+
"eslint-config-pluv": "^0.0.0",
|
|
18
|
+
"tsup": "^6.5.0",
|
|
19
|
+
"typescript": "^4.9.4"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
23
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
24
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
25
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AbstractWebSocketConfig } from "@pluv/io";
|
|
2
|
+
import { AbstractPlatform } from "@pluv/io";
|
|
3
|
+
import { CloudflareWebSocket } from "./CloudflareWebSocket";
|
|
4
|
+
|
|
5
|
+
export class CloudflarePlatform extends AbstractPlatform<WebSocket> {
|
|
6
|
+
public convertWebSocket(
|
|
7
|
+
webSocket: WebSocket,
|
|
8
|
+
config: AbstractWebSocketConfig
|
|
9
|
+
): CloudflareWebSocket {
|
|
10
|
+
return new CloudflareWebSocket(webSocket, config);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public parseData(data: string | ArrayBuffer): Record<string, any> {
|
|
14
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
15
|
+
|
|
16
|
+
const decoder = new TextDecoder("utf8");
|
|
17
|
+
|
|
18
|
+
return JSON.parse(decoder.decode(data));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public randomUUID(): string {
|
|
22
|
+
return crypto.randomUUID();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AbstractEventMap,
|
|
3
|
+
AbstractListener,
|
|
4
|
+
AbstractWebSocket,
|
|
5
|
+
AbstractWebSocketConfig,
|
|
6
|
+
} from "@pluv/io";
|
|
7
|
+
|
|
8
|
+
export interface CloudflareWebSocketEventMap {
|
|
9
|
+
close: CloseEvent;
|
|
10
|
+
message: MessageEvent;
|
|
11
|
+
open: Event;
|
|
12
|
+
error: ErrorEvent;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type CloudflareWebSocketConfig = AbstractWebSocketConfig;
|
|
16
|
+
|
|
17
|
+
export class CloudflareWebSocket extends AbstractWebSocket {
|
|
18
|
+
public webSocket: WebSocket;
|
|
19
|
+
|
|
20
|
+
public get readyState(): 0 | 1 | 2 | 3 {
|
|
21
|
+
return this.webSocket.readyState as 0 | 1 | 2 | 3;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
constructor(webSocket: WebSocket, config: CloudflareWebSocketConfig) {
|
|
25
|
+
const { room } = config;
|
|
26
|
+
|
|
27
|
+
super({ room });
|
|
28
|
+
|
|
29
|
+
this.webSocket = webSocket;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public addEventListener<TType extends keyof AbstractEventMap>(
|
|
33
|
+
type: TType,
|
|
34
|
+
handler: AbstractListener<TType>
|
|
35
|
+
) {
|
|
36
|
+
this.webSocket.addEventListener(type, handler as any);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public close(code?: number | undefined, reason?: string | undefined): void {
|
|
40
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
41
|
+
(readyState) => readyState === this.readyState
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
if (!canClose) return;
|
|
45
|
+
|
|
46
|
+
this.webSocket.close(code, reason);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public initialize(): Promise<() => undefined> {
|
|
50
|
+
this.webSocket.accept();
|
|
51
|
+
|
|
52
|
+
return Promise.resolve(() => undefined);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public send(message: string | ArrayBuffer | ArrayBufferView): void {
|
|
56
|
+
if (this.readyState !== this.OPEN) return;
|
|
57
|
+
|
|
58
|
+
this.webSocket.send(message);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public terminate(): void {
|
|
62
|
+
return this.webSocket.close(1011, "Terminated");
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { platformCloudflare } from "./platformCloudflare";
|