@dohaidang/locket_mod.js 0.0.0-stage → 0.0.3

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/index.d.ts ADDED
@@ -0,0 +1,82 @@
1
+ export interface DeviceProfileOptions {
2
+ autoEmulate?: boolean;
3
+ installationId?: string | null;
4
+ bundleId?: string | null;
5
+ appVersion?: string | null;
6
+ buildNumber?: string | null;
7
+ modelName?: string | null;
8
+ iosVersion?: string | null;
9
+ hardwareModel?: string | null;
10
+ customUserAgent?: string | null;
11
+ customAnalytics?: any;
12
+ appcheckToken?: string | null;
13
+ instanceIdToken?: string | null;
14
+ }
15
+
16
+ export class DeviceProfile {
17
+ constructor(options?: DeviceProfileOptions);
18
+ autoEmulate: boolean;
19
+ installationId: string;
20
+ bundleId: string;
21
+ appVersion: string;
22
+ buildNumber: string;
23
+ modelInfo: { name: string; model: string; hw: string; ios: string };
24
+ customUserAgent: string | null;
25
+ customAnalytics: any;
26
+ appcheckToken: string | null;
27
+ instanceIdToken: string | null;
28
+ get userAgent(): string;
29
+ get analyticsData(): any;
30
+ get defaultHeaders(): Record<string, string>;
31
+ setCustomDevice(options?: any): void;
32
+ enableAutoEmulation(): void;
33
+ toDict(): any;
34
+ static fromDict(data: any): DeviceProfile;
35
+ }
36
+
37
+ export class SessionManager {
38
+ static FIREBASE_API_KEY: string;
39
+ sessionPath: string;
40
+ device: DeviceProfile;
41
+ uid: string | null;
42
+ email: string | null;
43
+ idToken: string | null;
44
+ refreshToken: string | null;
45
+ expiresAt: number;
46
+ constructor(options?: { sessionPath?: string; device?: DeviceProfile | null });
47
+ load(): boolean;
48
+ save(): void;
49
+ login(email: string, password: string): Promise<any>;
50
+ refresh(): Promise<void>;
51
+ getValidToken(): Promise<string>;
52
+ }
53
+
54
+ export class LocketClient {
55
+ static API_BASE: string;
56
+ device: DeviceProfile;
57
+ session: SessionManager;
58
+ user: any;
59
+ moments: any;
60
+ friends: any;
61
+ chat: any;
62
+ constructor(options?: any);
63
+ setDevice(options?: any): void;
64
+ isAuthenticated(): boolean;
65
+ login(email: string, password: string): Promise<any>;
66
+ postApi(endpoint: string, payload?: any, options?: any): Promise<any>;
67
+ }
68
+
69
+ export class LocketError extends Error {}
70
+ export class AuthenticationError extends LocketError {}
71
+ export class LocketAPIError extends LocketError { statusCode: number | null; responseData: any; }
72
+ export class StorageUploadError extends LocketError {}
73
+
74
+ export class UserModule { constructor(client: LocketClient); [key: string]: any; }
75
+ export class MomentsModule { constructor(client: LocketClient); getMoments(options?: { filteredByUserId?: string | null; limit?: number; pageToken?: string | null }): Promise<{ moments: any[]; nextPageToken: string | null }>; [key: string]: any; }
76
+ export class FriendsModule { constructor(client: LocketClient); [key: string]: any; }
77
+ export class ChatModule { constructor(client: LocketClient); [key: string]: any; ReactionEmoji: any; }
78
+ export class NoteType { static [key: string]: string; }
79
+ export class ReactionEmoji { static [key: string]: any; }
80
+
81
+ export const MomentsModuleExport: any;
82
+ export {};
package/index.js ADDED
@@ -0,0 +1,24 @@
1
+ const { LocketClient } = require("./lib/client");
2
+ const { DeviceProfile } = require("./lib/device");
3
+ const { SessionManager } = require("./lib/session");
4
+ const { LocketError, AuthenticationError, LocketAPIError, StorageUploadError } = require("./lib/exceptions");
5
+ const { UserModule } = require("./lib/modules/user");
6
+ const { MomentsModule, NoteType } = require("./lib/modules/moments");
7
+ const { FriendsModule } = require("./lib/modules/friends");
8
+ const { ChatModule, ReactionEmoji } = require("./lib/modules/chat");
9
+
10
+ module.exports = {
11
+ LocketClient,
12
+ DeviceProfile,
13
+ SessionManager,
14
+ LocketError,
15
+ AuthenticationError,
16
+ LocketAPIError,
17
+ StorageUploadError,
18
+ UserModule,
19
+ MomentsModule,
20
+ NoteType,
21
+ FriendsModule,
22
+ ChatModule,
23
+ ReactionEmoji
24
+ };
package/lib/client.js ADDED
@@ -0,0 +1,86 @@
1
+ const { DeviceProfile } = require("./device");
2
+ const { SessionManager } = require("./session");
3
+ const { UserModule } = require("./modules/user");
4
+ const { MomentsModule } = require("./modules/moments");
5
+ const { FriendsModule } = require("./modules/friends");
6
+ const { ChatModule } = require("./modules/chat");
7
+ const { LocketAPIError } = require("./exceptions");
8
+ const { fetchWithTimeout } = require("./http");
9
+
10
+ class LocketClient {
11
+ static API_BASE = "https://api.locketcamera.com";
12
+
13
+ constructor(options = {}) {
14
+ const { sessionPath = "locket.js_session.json", device = null, autoEmulate = true, ...deviceKwargs } = options;
15
+ if (device !== null) {
16
+ this.device = device;
17
+ } else if (!autoEmulate || Object.keys(deviceKwargs).length) {
18
+ this.device = new DeviceProfile({ autoEmulate, ...deviceKwargs });
19
+ } else {
20
+ this.device = new DeviceProfile({ autoEmulate: true });
21
+ }
22
+ this.session = new SessionManager({ sessionPath, device: this.device });
23
+
24
+ this.user = new UserModule(this);
25
+ this.moments = new MomentsModule(this);
26
+ this.friends = new FriendsModule(this);
27
+ this.chat = new ChatModule(this);
28
+ }
29
+
30
+ setDevice(options = {}) {
31
+ const { autoEmulate = true, ...rest } = options;
32
+ if (autoEmulate) {
33
+ this.device.enableAutoEmulation();
34
+ } else {
35
+ this.device.setCustomDevice(rest);
36
+ }
37
+ this.session.save();
38
+ }
39
+
40
+ isAuthenticated() {
41
+ return Boolean(this.session.idToken && this.session.uid);
42
+ }
43
+
44
+ async login(email, password) {
45
+ return this.session.login(email, password);
46
+ }
47
+
48
+ async postApi(endpoint, payload = null, options = {}) {
49
+ const { timeoutMs = 30000 } = options || {};
50
+ const token = await this.session.getValidToken();
51
+ const url = `${LocketClient.API_BASE}/${endpoint}`;
52
+ const headers = {
53
+ ...this.device.defaultHeaders,
54
+ Authorization: `Bearer ${token}`,
55
+ "Content-Type": "application/json; charset=utf-8"
56
+ };
57
+ const body = {
58
+ data: {
59
+ ...(payload || {}),
60
+ analytics: this.device.analyticsData
61
+ }
62
+ };
63
+
64
+ let res;
65
+ try {
66
+ res = await fetchWithTimeout(url, { method: "POST", headers, body: JSON.stringify(body) }, timeoutMs);
67
+ } catch (e) {
68
+ throw new LocketAPIError(`Network request failed for ${endpoint}: ${e && e.message ? e.message : e}`, null, null);
69
+ }
70
+
71
+ let data;
72
+ try { data = await res.json(); } catch (e) { data = { status: res.status, error: res.statusText }; }
73
+
74
+ if (!res.ok) {
75
+ const errMsg = (data && (data.error || data.errors || data.message)) || res.statusText || `HTTP ${res.status}`;
76
+ throw new LocketAPIError(`API request to ${endpoint} failed: ${errMsg}`, res.status, data);
77
+ }
78
+ if (data && (data.error || data.errors)) {
79
+ const errMsg = data.error || data.errors;
80
+ throw new LocketAPIError(`API request to ${endpoint} failed: ${errMsg}`, (data && data.status) || res.status, data);
81
+ }
82
+ return (data && data.result) ? data.result : data;
83
+ }
84
+ }
85
+
86
+ module.exports = { LocketClient };
package/lib/device.js ADDED
@@ -0,0 +1,160 @@
1
+ const { randomUUID } = require("crypto");
2
+
3
+ const PRESET_MODELS = [
4
+ { model: "iPhone14,2", name: "iPhone 13 Pro", hw: "hw/iPhone14_2", ios: "17.4.1" },
5
+ { model: "iPhone14,5", name: "iPhone 13", hw: "hw/iPhone14_5", ios: "17.5.1" },
6
+ { model: "iPhone15,2", name: "iPhone 14 Pro", hw: "hw/iPhone15_2", ios: "17.6.1" },
7
+ { model: "iPhone15,3", name: "iPhone 14 Pro Max", hw: "hw/iPhone15_3", ios: "18.0.1" },
8
+ { model: "iPhone16,1", name: "iPhone 15 Pro", hw: "hw/iPhone16_1", ios: "18.1.0" }
9
+ ];
10
+
11
+ class DeviceProfile {
12
+ constructor(options = {}) {
13
+ const {
14
+ autoEmulate = true,
15
+ installationId = null,
16
+ bundleId = null,
17
+ appVersion = null,
18
+ buildNumber = null,
19
+ modelName = null,
20
+ iosVersion = null,
21
+ hardwareModel = null,
22
+ customUserAgent = null,
23
+ customAnalytics = null,
24
+ appcheckToken = null,
25
+ instanceIdToken = null
26
+ } = options;
27
+
28
+ this.autoEmulate = autoEmulate;
29
+ this._preset = PRESET_MODELS[Math.floor(Math.random() * PRESET_MODELS.length)];
30
+
31
+ this.installationId = installationId || randomUUID().replace(/-/g, "").slice(0, 22);
32
+ this.bundleId = bundleId || "com.locket.Locket";
33
+ this.appVersion = appVersion || "2.68.0";
34
+ this.buildNumber = buildNumber || "2523";
35
+
36
+ const hw = hardwareModel || this._preset.model;
37
+ const hwTag = hw.startsWith("hw/") ? hw : `hw/${hw}`;
38
+ this.modelInfo = {
39
+ name: modelName || this._preset.name,
40
+ model: hw,
41
+ hw: hwTag,
42
+ ios: iosVersion || this._preset.ios
43
+ };
44
+
45
+ this.customUserAgent = customUserAgent;
46
+ this.customAnalytics = customAnalytics;
47
+ this.appcheckToken = appcheckToken;
48
+ this.instanceIdToken = instanceIdToken;
49
+ }
50
+
51
+ get userAgent() {
52
+ if (!this.autoEmulate && this.customUserAgent) {
53
+ return this.customUserAgent;
54
+ }
55
+ return `${this.bundleId}/${this.appVersion} iPhone/${this.modelInfo.ios} ${this.modelInfo.hw}`;
56
+ }
57
+
58
+ get analyticsData() {
59
+ if (!this.autoEmulate && this.customAnalytics) {
60
+ return this.customAnalytics;
61
+ }
62
+ return {
63
+ ios_version: `${this.appVersion}.${this.buildNumber}`,
64
+ experiments: {
65
+ flag_18: { "@type": "type.googleapis.com/google.protobuf.Int64Value", value: "1203" },
66
+ flag_7: { "@type": "type.googleapis.com/google.protobuf.Int64Value", value: "800" },
67
+ flag_10: { "@type": "type.googleapis.com/google.protobuf.Int64Value", value: "505" }
68
+ },
69
+ installation_id: this.installationId,
70
+ platform: "ios"
71
+ };
72
+ }
73
+
74
+ get defaultHeaders() {
75
+ const headers = {
76
+ "User-Agent": this.userAgent,
77
+ "Content-Type": "application/json",
78
+ "Accept": "*/*",
79
+ "x-ios-bundle-identifier": this.bundleId
80
+ };
81
+ if (this.appcheckToken) {
82
+ headers["X-Firebase-AppCheck"] = this.appcheckToken;
83
+ }
84
+ if (this.instanceIdToken) {
85
+ headers["Firebase-Instance-Id-Token"] = this.instanceIdToken;
86
+ }
87
+ return headers;
88
+ }
89
+
90
+ setCustomDevice(options = {}) {
91
+ this.autoEmulate = false;
92
+ const {
93
+ installationId = null,
94
+ bundleId = null,
95
+ hardwareModel = null,
96
+ modelName = null,
97
+ iosVersion = null,
98
+ appVersion = null,
99
+ buildNumber = null,
100
+ customUserAgent = null,
101
+ customAnalytics = null,
102
+ appcheckToken = null,
103
+ instanceIdToken = null
104
+ } = options;
105
+ if (installationId !== null) this.installationId = installationId;
106
+ if (bundleId !== null) this.bundleId = bundleId;
107
+ if (appVersion !== null) this.appVersion = appVersion;
108
+ if (buildNumber !== null) this.buildNumber = buildNumber;
109
+ if (iosVersion !== null) this.modelInfo.ios = iosVersion;
110
+ if (modelName !== null) this.modelInfo.name = modelName;
111
+ if (hardwareModel !== null) {
112
+ this.modelInfo.model = hardwareModel;
113
+ this.modelInfo.hw = hardwareModel.startsWith("hw/") ? hardwareModel : `hw/${hardwareModel}`;
114
+ }
115
+ if (customUserAgent !== null) this.customUserAgent = customUserAgent;
116
+ if (customAnalytics !== null) this.customAnalytics = customAnalytics;
117
+ if (appcheckToken !== null) this.appcheckToken = appcheckToken;
118
+ if (instanceIdToken !== null) this.instanceIdToken = instanceIdToken;
119
+ }
120
+
121
+ enableAutoEmulation() {
122
+ this.autoEmulate = true;
123
+ this.customUserAgent = null;
124
+ this.customAnalytics = null;
125
+ }
126
+
127
+ toDict() {
128
+ return {
129
+ auto_emulate: this.autoEmulate,
130
+ installation_id: this.installationId,
131
+ bundle_id: this.bundleId,
132
+ app_version: this.appVersion,
133
+ build_number: this.buildNumber,
134
+ model_info: this.modelInfo,
135
+ custom_user_agent: this.customUserAgent,
136
+ custom_analytics: this.customAnalytics,
137
+ appcheck_token: this.appcheckToken,
138
+ instance_id_token: this.instanceIdToken
139
+ };
140
+ }
141
+
142
+ static fromDict(data) {
143
+ if (!data) return new DeviceProfile();
144
+ const inst = new DeviceProfile({
145
+ autoEmulate: data.auto_emulate !== undefined ? data.auto_emulate : true,
146
+ installationId: data.installation_id,
147
+ bundleId: data.bundle_id || "com.locket.Locket",
148
+ appVersion: data.app_version || "2.68.0",
149
+ buildNumber: data.build_number || "2523",
150
+ customUserAgent: data.custom_user_agent,
151
+ customAnalytics: data.custom_analytics,
152
+ appcheckToken: data.appcheck_token,
153
+ instanceIdToken: data.instance_id_token
154
+ });
155
+ if (data.model_info) inst.modelInfo = data.model_info;
156
+ return inst;
157
+ }
158
+ }
159
+
160
+ module.exports = { DeviceProfile };
@@ -0,0 +1,31 @@
1
+ class LocketError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = "LocketError";
5
+ }
6
+ }
7
+
8
+ class AuthenticationError extends LocketError {
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = "AuthenticationError";
12
+ }
13
+ }
14
+
15
+ class LocketAPIError extends LocketError {
16
+ constructor(message, statusCode = null, responseData = null) {
17
+ super(message);
18
+ this.name = "LocketAPIError";
19
+ this.statusCode = statusCode;
20
+ this.responseData = responseData;
21
+ }
22
+ }
23
+
24
+ class StorageUploadError extends LocketError {
25
+ constructor(message) {
26
+ super(message);
27
+ this.name = "StorageUploadError";
28
+ }
29
+ }
30
+
31
+ module.exports = { LocketError, AuthenticationError, LocketAPIError, StorageUploadError };
package/lib/http.js ADDED
@@ -0,0 +1,21 @@
1
+ const DEFAULT_TIMEOUT = 30000;
2
+
3
+ async function fetchWithTimeout(url, options = {}, timeoutMs = DEFAULT_TIMEOUT) {
4
+ const controller = new AbortController();
5
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
6
+ try {
7
+ const res = await fetch(url, { ...options, signal: controller.signal });
8
+ return res;
9
+ } finally {
10
+ clearTimeout(timer);
11
+ }
12
+ }
13
+
14
+ async function fetchJson(url, options = {}, timeoutMs = DEFAULT_TIMEOUT) {
15
+ const res = await fetchWithTimeout(url, options, timeoutMs);
16
+ let data;
17
+ try { data = await res.json(); } catch (e) { data = null; }
18
+ return { res, data };
19
+ }
20
+
21
+ module.exports = { fetchWithTimeout, fetchJson, DEFAULT_TIMEOUT };