Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fastly device lookup #24

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Sources/Compute/Fastly/FastlyDevice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// FastlyDevice.swift
//
//
// Created by Andrew Barba on 3/19/24.
//

import ComputeRuntime

extension Fastly {
public struct Device: Sendable {

public struct DeviceLookup: Codable, Sendable {
public let name: String?
public let brand: String?
public let model: String?
public let hardwareType: String?
public let isDesktop: Bool?
public let isGameConsole: Bool?
public let isMediaPlayer: Bool?
public let isMobile: Bool?
public let isSmartTV: Bool?
public let isTablet: Bool?
public let isTouchscreen: Bool?
}

public static func lookup(userAgent: String) throws -> DeviceLookup {
return try wasiDecode(maxBufferLength: maxIpLookupLength) {
fastly_device__device_detection_lookup(userAgent, userAgent.utf8.count, $0, $1, &$2)
}
}

private init() {}
}
}
8 changes: 8 additions & 0 deletions Sources/Compute/Fastly/FastlyStubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,12 @@ func fastly_cache__cache_get_body(

func fastly_cache__cache_transaction_cancel(_ handle: WasiHandle) -> Int32 { fatalError() }

func fastly_device__device_detection_lookup(
_ user_agent: UnsafePointer<CChar>!,
_ user_agent_len: Int,
_ buf: UnsafeMutablePointer<CChar>!,
_ buf_len: Int,
_ nwritten: UnsafeMutablePointer<Int>!
) -> Int32 { fatalError() }

#endif
6 changes: 6 additions & 0 deletions Sources/ComputeRuntime/include/ComputeRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,5 +335,11 @@ int fastly_cache__cache_get_body(WasiHandle handle, uint32_t options_mask,
CacheGetBodyConfig *config,
WasiHandle *ret);

/* FASTLY_DEVICE */

WASM_IMPORT("fastly_device_detection", "lookup")
int fastly_device__device_detection_lookup(const char *user_agent, size_t user_agent_len, const char *buf,
size_t buf_len, size_t *nwritten);

#pragma GCC diagnostic pop
#endif /* ComputeRuntime_h */
Loading